Joint plot with regression line and classes by hue - seaborn

I have the following dataframe
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
I would like to plot the regression line of the overall data, but also the scatter points by hue (in this case by Product) in the same chart.
I can get the regression line by:
g = sns.jointplot(y='Sales', x='Price', data=df, kind='reg', scatter = False)
And I can get the scatter by:
g = sns.scatterplot(y='Sales', x='Price', data=df, hue='Product')
But there are two different charts. Anyway that I can combine the two commands?

You have to tell the scatterplot in which axis object you want to plot. Options for a seaborn jointplot are the main plot area ax_joint or the two minor plot areas ax_marg_x and ax_marg_y.
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
g = sns.jointplot(y='Sales', x='Price', data=df, kind='reg', scatter = False)
sns.scatterplot(y='Sales', x='Price', data=df, hue="Product", ax=g.ax_joint)
plt.show()
Sample output:

To extend on Mr. T's answer, you could also do something like this to keep the kde marginal plots of the jointplot:
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
g = sns.jointplot(y='Sales', x='Price', data=df, hue="Product", alpha=0.5, xlim=(0.5,7.5), ylim=(-50, 550))
g1 = sns.regplot(y='Sales', x='Price', data=df, scatter=False, ax=g.ax_joint)
regline = g1.get_lines()[0]
regline.set_color('red')
regline.set_zorder(5)
plt.show()

Related

pyqtgrapth cololbar matching to data valuse

I am a new pyqt user and not quit familar with it.
I have a dataset min=-50000, max=100000. I need to plot it using pyqtgrapth. However the color bar I am using dose not match with the actual values of the data.
I have to add, data normalized to one before ploting.
Data = Data/np.max(Data)
Here are my codes to plot them:
entstops = [0, 0.005, 0.01, 0.025, 0.05,0.1, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8,0.9,1.0]er code here
enself.colors = 255 * array(
[[1,1,1,0.7],[1, 1, 1, 0.2],[1, 1, 1, 1], [0, 0, 0, 0.3], [1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [1, 1, 0.5, 1], [1, 0.89, 0.03, 1],
[1, 1, 0, 1], [1, 0.165, 0, 1],[1, 0, 0, 1],[0.128,0,0,1], [1, 1, 1, 1]])
entercolormap = pg.ColorMap(stops, self.colors)
lut = colormap.getLookupTable(-0.1, 1.0, 512)
self.frog_preview_draw["data"] = pg.ImageItem()
self.frog_preview_draw["data"].setLookupTable(lut)
What I am expecting is, having more colors around (-0.1-0,1) and less colors for bigger numeber, because I would like to see the noise of my data.
Thank you

How can I plot different types of seaborn plots on different x ticks?

I want to have multiple types of seaborn plots using the same y axis but with different x coordinates (see image below).
I've tried doing this multiple different ways with specifying the X-axis coordinates differently but can't seem to get it to work.
Here is an example of almost working code
x=[1,2,3,3,3,4,4,5,5,6] # first violin
y=[4,4,5,5,5,5,6] # second violin
z=[5,5,6] # swarmplot over second violin
for data,label in [(x,'x'),(y,'y'),(z,'z')]:
for i in data:
c2v['value'].append(i)
c2v['key'].append(label)
data=pd.DataFrame(c2v)
data.head()
print(data.loc[data.key=='z'])
fig,ax=plt.subplots(1,figsize=(5,5),dpi=200)
ax = sns.violinplot(data=data.loc[data.key.isin(['x','y'])], x='key', y='value',palette=['honeydew','lightgreen'])
sns.swarmplot(x=['swarmplot']*len(data), y=data['value'], order=ax.get_xticklabels() + ['swarmplot'], ax=ax) #.loc[data.key=='z',:]
ax.set_xlabel('')
It produces the following image:
However, it is plotting all values associated with x/y/z instead of just z. When I slice the dataframe to only 'z' in the swarmplot as below, I get an error:
sns.swarmplot(x=['swarmplot']*len(data), y=data.loc[data.key=='z',:]['value'], order=ax.get_xticklabels() + ['swarmplot'], ax=ax)
KeyError: 'swarmplot'
Any suggestions?
To draw a second plot onto the same x-axis, you can use order= giving a list of existing tick labels, appending the new labels.
Here is an example:
import seaborn as sns
tips = sns.load_dataset('tips')
ax = sns.swarmplot(data=tips, x='day', y='total_bill')
sns.violinplot(x=['violin']*len(tips), y=tips['total_bill'], order=ax.get_xticklabels() + ['violin'], ax=ax)
ax.set_xlabel('')
The problem with the code in the new question, is that the x= and y= of the swarmplot need the same number of elements. It also seems the swarmplot resets the y limits, so I added some code to readjust those:
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
x = [1, 2, 3, 3, 3, 4, 4, 5, 5, 6] # first violin
y = [4, 4, 5, 5, 5, 5, 6] # second violin
z = [5, 5, 6] # swarmplot over second violin
data = pd.DataFrame({'value': np.concatenate([x, y, z]),
'key': ['x'] * len(x) + ['y'] * len(y) + ['z'] * len(z)})
fig, ax = plt.subplots(1, figsize=(5, 5))
ax = sns.violinplot(data=data.loc[data.key.isin(['x', 'y'])], x='key', y='value', palette=['honeydew', 'lightgreen'])
ymin1, ymax1 = ax.get_ylim()
swarm_data = data.loc[data.key == 'z', :]['value']
sns.swarmplot(x=['swarmplot'] * len(swarm_data), y=swarm_data, order=ax.get_xticklabels() + ['swarmplot'], ax=ax)
ymin2, ymax2 = ax.get_ylim()
ax.set_ylim(min(ymin1, ymin2), max(ymax1, ymax2))
ax.set_xlabel('')
ax.set_xticks(np.arange(3))
ax.set_xticklabels(['x', 'y', 'swarmplot'])
plt.show()
You can simplify things by directly using the data without creating a dataframe:
x = [1, 2, 3, 3, 3, 4, 4, 5, 5, 6] # first violin
y = [4, 4, 5, 5, 5, 5, 6] # second violin
z = [5, 5, 6] # swarmplot over second violin
fig, ax = plt.subplots(1, figsize=(5, 5))
ax = sns.violinplot(x=['x']*len(x) + ['y']*len(y), y=x + y, palette=['honeydew', 'lightgreen'])
ymin1, ymax1 = ax.get_ylim()
sns.swarmplot(x=['swarmplot'] * len(z), y=z, order=ax.get_xticklabels() + ['swarmplot'], ax=ax)
ymin2, ymax2 = ax.get_ylim()
ax.set_ylim(min(ymin1, ymin2), max(ymax1, ymax2))
ax.set_xticks(np.arange(3))
ax.set_xticklabels(['x', 'y', 'swarmplot'])
plt.show()

Why does this simple LightGBM binary classifier perform poorly?

I tried to train a LightGBM binary classifier using the Python API the relation -
if feature > 5, then 1 else 0
import pandas as pd
import numpy as np
import lightgbm as lgb
x_train = pd.DataFrame([4, 7, 2, 6, 3, 1, 9])
y_train = pd.DataFrame([0, 1, 0, 1, 0, 0, 1])
x_test = pd.DataFrame([8, 2])
y_test = pd.DataFrame([1, 0])
lgb_train = lgb.Dataset(x_train, y_train)
lgb_eval = lgb.Dataset(x_test, y_test, reference=lgb_train)
params = { 'objective': 'binary', 'metric': {'binary_logloss', 'auc'}}
gbm = lgb.train(params, lgb_train, valid_sets=lgb_eval)
y_pred = gbm.predict(x_test, num_iteration=gbm.best_iteration)
y_pred
array([0.42857143, 0.42857143])
np.where((y_pred > 0.5), 1, 0)
array([0, 0])
Clearly it failed to predict the first test 8. Can anyone see what went wrong?
LightGBM's parameter defaults are set with the expectation of moderate-sized training data, and might not work well on extremely small datasets like the one in this question.
There are two in particular that are impacting your result:
min_data_in_leaf: minimum number of samples that must fall into a leaf node
min_sum_hessian_in_leaf: basically, the minimum contribution to the loss function for one leaf node
Setting these to the lowest possible values can force LightGBM to overfit to such a small dataset.
import pandas as pd
import numpy as np
import lightgbm as lgb
x_train = pd.DataFrame([4, 7, 2, 6, 3, 1, 9])
y_train = pd.DataFrame([0, 1, 0, 1, 0, 0, 1])
x_test = pd.DataFrame([8, 2])
y_test = pd.DataFrame([1, 0])
lgb_train = lgb.Dataset(x_train, y_train)
lgb_eval = lgb.Dataset(x_test, y_test, reference=lgb_train)
params = {
'objective': 'binary',
'metric': {'binary_logloss', 'auc'},
'min_data_in_leaf': 1,
'min_sum_hessian_in_leaf': 0
}
gbm = lgb.train(params, lgb_train, valid_sets=lgb_eval)
y_pred = gbm.predict(x_test, num_iteration=gbm.best_iteration)
y_pred
# array([6.66660313e-01, 1.89048958e-05])
np.where((y_pred > 0.5), 1, 0)
# array([1, 0])
For details on all the parameters and their defaults, see https://lightgbm.readthedocs.io/en/latest/Parameters.html.

Why does adding dropout layers work on validation set, but not on test set?

I'm working on a convolutional neural network in TensorFlow and having trouble with the dropout layers. As recommended, I'm passing a keep_probability placeholder to the graph and setting the value to 0.5 during training, and 1.0 during validation and testing. When observing the training process, the results are good for the validation set. However, when I test the network after training, the network fails.
UPDATE: When I say that the network fails, I mean that the network no longer segments the images correctly. During validation the network gets an mIoU of around 80%, but when testing it falls down to around 40% and classifies all the pixels into just one of the classes. Before the dropout layers were added, both validation and test set got an mIoU of around 80%.
I do not understand why the network is failing on the test set when it works on the validation set?
I've added the code for training, testing and for the graph itself.
Code for training the network:
with tf.Graph().as_default():
#Probablitity that the neuron's output will be kept during dropout
keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
global_step = tf.Variable(0, trainable=False)
images, labels = Inputs.datasetInputs(image_filenames, label_filenames, FLAGS.batch_size)
val_images, val_labels = Inputs.datasetInputs(val_image_filenames, val_label_filenames, FLAGS.batch_size)
train_data_node = tf.placeholder(tf.float32, shape=[FLAGS.batch_size, FLAGS.image_h, FLAGS.image_w, 3])
train_labels_node = tf.placeholder(tf.int64, shape=[FLAGS.batch_size, FLAGS.image_h, FLAGS.image_w, 1])
phase_train = tf.placeholder(tf.bool, name='phase_train')
logits = model.inference(train_data_node, phase_train, FLAGS.batch_size, keep_probability) #tensor, nothing calculated yet
loss = model.cal_loss(logits, train_labels_node)
# Build a Graph that trains the model with one batch of examples and updates the model parameters.
train_op = model.train(loss, global_step)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
""" Starting iterations to train the network """
for step in range(startstep, startstep + FLAGS.max_steps):
image_batch ,label_batch = sess.run(fetches=[images, labels])
# since we still use mini-batches in eval, still set bn-layer phase_train = True
feed_dict = {
train_data_node: image_batch,
train_labels_node: label_batch,
phase_train: True,
keep_probability: 0.5
}
_, loss_value = sess.run(fetches=[train_op, loss], feed_dict=feed_dict)
if step % 10 == 0:
num_examples_per_step = FLAGS.batch_size
examples_per_sec = num_examples_per_step / duration
sec_per_batch = float(duration)
# eval current training batch pre-class accuracy
pred = sess.run(fetches=logits, feed_dict=feed_dict)
Utils.per_class_acc(pred, label_batch)
if step % 100 == 0 or (step + 1) == FLAGS.max_steps:
""" Validate training by running validation dataset """
total_val_loss = 0.0
hist = np.zeros((FLAGS.num_class, FLAGS.num_class))
for test_step in range(TEST_ITER):
val_images_batch, val_labels_batch = sess.run(fetches=[val_images, val_labels])
feed_dict = {
train_data_node: val_images_batch,
train_labels_node: val_labels_batch,
phase_train: True,
keep_probability: 1.0 #During testing droput should be turned off -> 100% chance for keeping variable
}
_val_loss, _val_pred = sess.run(fetches=[loss, logits], feed_dict=feed_dict)
(...)
Code for testing the network:
keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
image_filenames, label_filenames = Inputs.get_filename_list(FLAGS.test_dir)
test_data_node = tf.placeholder(tf.float32, shape=[testing_batch_size, FLAGS.image_h, FLAGS.image_w, FLAGS.image_c]) #360, 480, 3
test_labels_node = tf.placeholder(tf.int64, shape=[FLAGS.test_batch_size, FLAGS.image_h, FLAGS.image_w, 1])
phase_train = tf.placeholder(tf.bool, name='phase_train')
logits = model.inference(test_data_node, phase_train, testing_batch_size, keep_probability)
loss = model.cal_loss(logits, test_labels_node)
pred = tf.argmax(logits, dimension=3)
with tf.Session() as sess:
# Load checkpoint
saver.restore(sess, FLAGS.model_ckpt_dir)
images, labels = Inputs.get_all_test_data(image_filenames, label_filenames)
threads = tf.train.start_queue_runners(sess=sess)
hist = np.zeros((FLAGS.num_class, FLAGS.num_class))
step=0
for image_batch, label_batch in zip(images, labels):
feed_dict = { #maps graph elements to values
test_data_node: image_batch,
test_labels_node: label_batch,
phase_train: False,
keep_probability: 1.0 #During testing droput should be turned off -> 100% chance for keeping variable
}
dense_prediction, im = sess.run(fetches=[logits, pred], feed_dict=feed_dict)
(...)
The graph:
def inference(images, phase_train, batch_size, keep_prob):
conv1_1 = conv_layer_with_bn(images, [7, 7, images.get_shape().as_list()[3], 64], phase_train, name="conv1_1")
conv1_2 = conv_layer_with_bn(conv1_1, [7, 7, 64, 64], phase_train, name="conv1_2")
dropout1 = tf.layers.dropout(conv1_2, rate=(1-keep_prob), training=phase_train, name="dropout1")
pool1, pool1_indices = tf.nn.max_pool_with_argmax(dropout1, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME', name='pool1')
conv2_1 = conv_layer_with_bn(pool1, [7, 7, 64, 64], phase_train, name="conv2_1")
conv2_2 = conv_layer_with_bn(conv2_1, [7, 7, 64, 64], phase_train, name="conv2_2")
dropout2 = tf.layers.dropout(conv2_2, rate=(1-keep_prob), training=phase_train, name="dropout2")
pool2, pool2_indices = tf.nn.max_pool_with_argmax(dropout2, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME', name='pool2')
conv3_1 = conv_layer_with_bn(pool2, [7, 7, 64, 64], phase_train, name="conv3_1")
conv3_2 = conv_layer_with_bn(conv3_1, [7, 7, 64, 64], phase_train, name="conv3_2")
conv3_3 = conv_layer_with_bn(conv3_2, [7, 7, 64, 64], phase_train, name="conv3_3")
dropout3 = tf.layers.dropout(conv3_3, rate=(1-keep_prob), training=phase_train, name="dropout3")
pool3, pool3_indices = tf.nn.max_pool_with_argmax(dropout3, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME', name='pool3')
conv4_1 = conv_layer_with_bn(pool3, [7, 7, 64, 64], phase_train, name="conv4_1")
conv4_2 = conv_layer_with_bn(conv4_1, [7, 7, 64, 64], phase_train, name="conv4_2")
conv4_3 = conv_layer_with_bn(conv4_2, [7, 7, 64, 64], phase_train, name="conv4_3")
dropout4 = tf.layers.dropout(conv4_3, rate=(1-keep_prob), training=phase_train, name="dropout4")
pool4, pool4_indices = tf.nn.max_pool_with_argmax(dropout4, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME', name='pool4')
conv5_1 = conv_layer_with_bn(pool4, [7, 7, 64, 64], phase_train, name="conv5_1")
conv5_2 = conv_layer_with_bn(conv5_1, [7, 7, 64, 64], phase_train, name="conv5_2")
conv5_3 = conv_layer_with_bn(conv5_2, [7, 7, 64, 64], phase_train, name="conv5_3")
dropout5 = tf.layers.dropout(conv5_3, rate=(1-keep_prob), training=phase_train, name="dropout5")
pool5, pool5_indices = tf.nn.max_pool_with_argmax(dropout5, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME', name='pool5')
""" End of encoder """
""" Start decoder """
dropout5_decode = tf.layers.dropout(pool5, rate=(1-keep_prob), training=phase_train, name="dropout5_decode")
upsample5 = deconv_layer(dropout5_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h//16, FLAGS.image_w//16, 64], 2, "up5")
conv_decode5_1 = conv_layer_with_bn(upsample5, [7, 7, 64, 64], phase_train, True, name="conv_decode5_1")
conv_decode5_2 = conv_layer_with_bn(conv_decode5_1, [7, 7, 64, 64], phase_train, True, name="conv_decode5_2")
conv_decode5_3 = conv_layer_with_bn(conv_decode5_2, [7, 7, 64, 64], phase_train, True, name="conv_decode5_3")
dropout4_decode = tf.layers.dropout(conv_decode5_3, rate=(1-keep_prob), training=phase_train, name="dropout4_decode")
upsample4 = deconv_layer(dropout4_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h//8, FLAGS.image_w//8, 64], 2, "up4")
conv_decode4_1 = conv_layer_with_bn(upsample4, [7, 7, 64, 64], phase_train, True, name="conv_decode4_1")
conv_decode4_2 = conv_layer_with_bn(conv_decode4_1, [7, 7, 64, 64], phase_train, True, name="conv_decode4_2")
conv_decode4_3 = conv_layer_with_bn(conv_decode4_2, [7, 7, 64, 64], phase_train, True, name="conv_decode4_3")
dropout3_decode = tf.layers.dropout(conv_decode4_3, rate=(1-keep_prob), training=phase_train, name="dropout3_decode")
upsample3 = deconv_layer(dropout3_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h//4, FLAGS.image_w//4, 64], 2, "up3")
conv_decode3_1 = conv_layer_with_bn(upsample3, [7, 7, 64, 64], phase_train, True, name="conv_decode3_1")
conv_decode3_2 = conv_layer_with_bn(conv_decode3_1, [7, 7, 64, 64], phase_train, True, name="conv_decode3_2")
conv_decode3_3 = conv_layer_with_bn(conv_decode3_2, [7, 7, 64, 64], phase_train, True, name="conv_decode3_3")
dropout2_decode = tf.layers.dropout(conv_decode3_3, rate=(1-keep_prob), training=phase_train, name="dropout2_decode")
upsample2= deconv_layer(dropout2_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h//2, FLAGS.image_w//2, 64], 2, "up2")
conv_decode2_1 = conv_layer_with_bn(upsample2, [7, 7, 64, 64], phase_train, True, name="conv_decode2_1")
conv_decode2_2 = conv_layer_with_bn(conv_decode2_1, [7, 7, 64, 64], phase_train, True, name="conv_decode2_2")
dropout1_decode = tf.layers.dropout(conv_decode2_2, rate=(1-keep_prob), training=phase_train, name="dropout1_deconv")
upsample1 = deconv_layer(dropout1_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h, FLAGS.image_w, 64], 2, "up1")
conv_decode1_1 = conv_layer_with_bn(upsample1, [7, 7, 64, 64], phase_train, True, name="conv_decode1_1")
conv_decode1_2 = conv_layer_with_bn(conv_decode1_1, [7, 7, 64, 64], phase_train, True, name="conv_decode1_2")
""" End of decoder """
""" Start Classify """
# output predicted class number (2)
with tf.variable_scope('conv_classifier') as scope:
shape=[1, 1, 64, FLAGS.num_class]
kernel = _variable_with_weight_decay('weights', shape=shape, initializer=tf.contrib.layers.variance_scaling_initializer(), #orthogonal_initializer()
wd=None)
conv = tf.nn.conv2d(conv_decode1_2, kernel, [1, 1, 1, 1], padding='SAME')
biases = _variable_on_cpu('biases', [FLAGS.num_class], tf.constant_initializer(0.0))
conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name) #tf.nn.bias_add is an activation function. Simple add that specifies 1-D tensor bias
#logit = conv_classifier = prediction
return conv_classifier
I eventually figured out that my issue came from problems with batch normalization not being implemented correctly.
The issue was that I did not update the moving_mean and moving_variance correctly. This is pointed out in the TensorFlow docs for batch_norm:
Note: when training, the moving_mean and moving_variance need to be
updated. By default the update ops are placed in
tf.GraphKeys.UPDATE_OPS, so they need to be added as a dependency to
the train_op.
My code, therefore, looks like this:
def training(loss):
global_step = tf.Variable(0, name='global_step', trainable=False)
#This motif is needed to hook up the batch_norm updates to the training
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
if(FLAGS.optimizer == "SGD"):
print("Running with SGD optimizer")
optimizer = tf.train.GradientDescentOptimizer(0.1)
elif(FLAGS.optimizer == "adam"):
print("Running with adam optimizer")
optimizer = tf.train.AdamOptimizer(0.001)
elif(FLAGS.optimizer == "adagrad"):
print("Running with adagrad optimizer")
optimizer = tf.train.AdagradOptimizer(0.01)
else:
raise ValueError("optimizer was not recognized.")
train_op = optimizer.minimize(loss=loss, global_step=global_step)
return train_op, global_step
Full implementation of my network can be seen here: https://github.com/mathildor/TF-SegNet

Tensorflow - Dynamic Slicing of Images

I previously asked this question, but after some investigation of the problem it appears I may have just gone down the wrong path for what I am trying to achieve.
Dynamic image cropping in Tensorflow
I thought maybe this might be a better path to try. But the part I can't figure out is what I should put for the size parameter on the slice operation. Fundamentally, what I am trying to achieve is having the capability to dynamically decide how to crop an image and then crop it and then continue with those cropped images in my computation graph. Feel free to offer an alternative if this seems to be an inefficient way to go about this.
import numpy as np
import tensorflow as tf
img1 = np.random.random([400, 600, 3])
img2 = np.random.random([400, 600, 3])
img3 = np.random.random([400, 600, 3])
images = [img1, img2, img3]
img1_crop = [100, 100, 100, 100]
img2_crop = [200, 150, 100, 100]
img3_crop = [150, 200, 100, 100]
crop_values = [img1_crop, img2_crop, img3_crop]
x = tf.placeholder(tf.float32, shape=[None, 400, 600, 3])
i = tf.placeholder(tf.int32, shape=[None, 4])
y = tf.slice(x, i, size="Not sure what to put here")
# initialize
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# run
result = sess.run(y, feed_dict={x: images, i: crop_values})
print(result)
Instead of using tf.slice (which doesn't let you operate on a batch), I recommend using tf.image.extract_glimpse. Here is a toy sample program that operates in a batch:
import tensorflow as tf
import numpy as np
NUM_IMAGES = 2
NUM_CHANNELS = 1
CROP_SIZE = [3, 4]
IMG_HEIGHT=10
IMG_WIDTH=10
# Fake input data, but ordered so we can look at the printed values and
# map them back. The values of the first image are:
# array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
# [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
# [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
# [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
# [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
# [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
# [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
# [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
# [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
image1 = np.reshape(
np.array(xrange(NUM_IMAGES * IMG_HEIGHT * IMG_WIDTH * NUM_CHANNELS)),
[NUM_IMAGES, IMG_HEIGHT, IMG_WIDTH, NUM_CHANNELS])
# We use normalized=False to use pixel indexing.
# normalized=True means centers are specified between [0,1).
image1_center = [0, 0] # The center of the crop is ~ the center of the image.
image2_center = [3, 5] # Offset down & right in the image.
img = tf.placeholder(tf.float32, shape=[NUM_IMAGES, IMG_HEIGHT, IMG_WIDTH, NUM_CHANNELS], name="img")
size = tf.placeholder(tf.int32, shape=[2], name="crop_size")
centers = tf.placeholder(tf.float32, shape=[NUM_IMAGES, 2], name="centers")
output = tf.image.extract_glimpse(img, size, centers, normalized=False)
sess = tf.Session()
feed_dict = {
img: image1,
size: CROP_SIZE,
centers: [image1_center, image2_center],
}
print sess.run(output, feed_dict=feed_dict)
If you would like to extract multiple sizes (and even multiple glimpses per image), check out tf.image.crop_and_resize.
Docs: https://www.tensorflow.org/api_docs/python/image/cropping#extract_glimpse

Resources