Python numpy 模块,expand_dims() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.expand_dims()。
def encode_jpeg(arr):
assert arr.dtype == np.uint8
# simulate multi-channel array for single channel arrays
if len(arr.shape) == 3:
arr = np.expand_dims(arr, 3) # add channels to end of x,y,z
arr = arr.transpose((3,2,1,0)) # channels, z, y, x
reshaped = arr.reshape(arr.shape[3] * arr.shape[2], arr.shape[1] * arr.shape[0])
if arr.shape[0] == 1:
img = Image.fromarray(reshaped, mode='L')
elif arr.shape[0] == 3:
img = Image.fromarray(reshaped, mode='RGB')
else:
raise ValueError("Number of image channels should be 1 or 3. Got: {}".format(arr.shape[3]))
f = io.BytesIO()
img.save(f, "JPEG")
return f.getvalue()
def model(self, features, labels):
x = features["observation"]
x = tf.contrib.layers.convolution2d(x, 2, kernel_size=[3, 3], stride=[2, 2], activation_fn=tf.nn.elu)
x = tf.contrib.layers.convolution2d(x, 2, kernel_size=[3, 3], stride=[2, 2], activation_fn=tf.nn.elu)
actions = tf.one_hot(tf.reshape(features["action"],[-1]), depth=6, on_value=1.0, off_value=0.0, axis=1)
x = tf.concat(1, [tf.contrib.layers.flatten(x), actions])
x = tf.contrib.layers.fully_connected(x, 100, activation_fn=tf.nn.elu)
x = tf.contrib.layers.fully_connected(x, 100, activation_fn=tf.nn.elu)
logits = tf.contrib.layers.fully_connected(x, 1, activation_fn=None)
prediction = tf.sigmoid(logits, name="prediction")
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits, tf.expand_dims(labels, axis=1)),name="loss")
train_op = tf.contrib.layers.optimize_loss(
loss, tf.contrib.framework.get_global_step(), optimizer='Adam',
learning_rate=self.learning_rate)
tf.add_to_collection('prediction', prediction)
tf.add_to_collection('loss', loss)
return prediction, loss, train_op
def get_image_descriptor_for_image(image, model):
im = cv2.resize(image, (224, 224)).astype(np.float32)
dim_ordering = K.image_dim_ordering()
if dim_ordering == 'th':
# 'RGB'->'BGR'
im = im[::-1, :, :]
# Zero-center by mean pixel
im[0, :, :] -= 103.939
im[1, :, :] -= 116.779
im[2, :, :] -= 123.68
else:
# 'RGB'->'BGR'
im = im[:, :, ::-1]
# Zero-center by mean pixel
im[:, :, 0] -= 103.939
im[:, :, 1] -= 116.779
im[:, :, 2] -= 123.68
im = im.transpose((2, 0, 1))
im = np.expand_dims(im, axis=0)
inputs = [K.learning_phase()] + model.inputs
_convout1_f = K.function(inputs, [model.layers[33].output])
return _convout1_f([0] + [im])
def get_freqs (signals, nbins=0):
""" extracts relative fft frequencies and bins them in n bins
:param signals: 1D or 2D signals
:param nbins: number of bins used as output (default: maximum possible)
"""
if signals.ndim == 1: signals = np.expand_dims(signals,0)
sfreq = use_sfreq
if nbins == 0: nbins = int(sfreq/2)
nsamp = float(signals.shape[1])
assert nsamp/2 >= nbins, 'more bins than fft results'
feats = np.zeros((int(signals.shape[0]),nbins),dtype='float32')
w = (fft(signals,axis=1)).real
for i in np.arange(nbins):
feats[:,i] = np.sum(np.abs(w[:,np.arange(i*nsamp/sfreq,(i+1)*nsamp/sfreq, dtype=int)]),axis=1)
sum_abs_pow = np.sum(feats,axis=1)
feats = (feats.T / sum_abs_pow).T
return feats
def pull_item(self, index):
img_id = self.ids[index]
target = ET.parse(self._annopath % img_id).getroot()
img = cv2.imread(self._imgpath % img_id)
height, width, channels = img.shape
if self.target_transform is not None:
target = self.target_transform(target, width, height)
if self.transform is not None:
target = np.array(target)
img, boxes, labels = self.transform(img, target[:, :4], target[:, 4])
# to rgb
img = img[:, :, (2, 1, 0)]
# img = img.transpose(2, 0, 1)
target = np.hstack((boxes, np.expand_dims(labels, axis=1)))
return torch.from_numpy(img).permute(2, 0, 1), target, height, width
# return torch.from_numpy(img), target, height, width
def model(self, features, labels):
x = features["observation"]
x = tf.contrib.layers.convolution2d(x, 2, kernel_size=[3, 3], stride=[2, 2], activation_fn=tf.nn.elu)
x = tf.contrib.layers.convolution2d(x, 2, kernel_size=[3, 3], stride=[2, 2], activation_fn=tf.nn.elu)
x = tf.contrib.layers.flatten(x)
x = tf.contrib.layers.fully_connected(x, 100, activation_fn=tf.nn.elu)
x = tf.contrib.layers.fully_connected(x, 100, activation_fn=tf.nn.elu)
logits = tf.contrib.layers.fully_connected(x, 1, activation_fn=None)
prediction = tf.sigmoid(logits)
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits, tf.expand_dims(labels, axis=1)))
train_op = tf.contrib.layers.optimize_loss(
loss, tf.contrib.framework.get_global_step(), optimizer='Adam',
learning_rate=0.01)
tf.add_to_collection('prediction', prediction)
tf.add_to_collection('loss', loss)
return prediction, loss, train_op
def normalize_bitmap(bitmap):
# pad the bitmap to make it squared
pad_size = abs(bitmap.shape[0]-bitmap.shape[1]) // 2
if bitmap.shape[0] < bitmap.shape[1]:
pad_dims = ((pad_size, pad_size), (0, 0))
else:
pad_dims = ((0, 0), (pad_size, pad_size))
bitmap = np.lib.pad(bitmap, pad_dims, mode='constant', constant_values=255)
# rescale and add empty border
bitmap = scipy.misc.imresize(bitmap, (64 - 4*2, 64 - 4*2))
bitmap = np.lib.pad(bitmap, ((4, 4), (4, 4)), mode='constant', constant_values=255)
assert bitmap.shape == (64, 64)
bitmap = np.expand_dims(bitmap, axis=0)
assert bitmap.shape == (1, 64, 64)
return bitmap
def get_feature_map_4(model, im):
im = im.astype(np.float32)
dim_ordering = K.image_dim_ordering()
if dim_ordering == 'th':
# 'RGB'->'BGR'
im = im[::-1, :, :]
# Zero-center by mean pixel
im[0, :, :] -= 103.939
im[1, :, :] -= 116.779
im[2, :, :] -= 123.68
else:
# 'RGB'->'BGR'
im = im[:, :, ::-1]
# Zero-center by mean pixel
im[:, :, 0] -= 103.939
im[:, :, 1] -= 116.779
im[:, :, 2] -= 123.68
im = im.transpose((2, 0, 1))
im = np.expand_dims(im, axis=0)
inputs = [K.learning_phase()] + model.inputs
_convout1_f = K.function(inputs, [model.layers[23].output])
feature_map = _convout1_f([0] + [im])
feature_map = np.array([feature_map])
feature_map = feature_map[0, 0, 0, :, :, :]
return feature_map
def get_conv_image_descriptor_for_image(image, model):
im = cv2.resize(image, (224, 224)).astype(np.float32)
dim_ordering = K.image_dim_ordering()
if dim_ordering == 'th':
# 'RGB'->'BGR'
im = im[::-1, :, :]
# Zero-center by mean pixel
im[0, :, :] -= 103.939
im[1, :, :] -= 116.779
im[2, :, :] -= 123.68
else:
# 'RGB'->'BGR'
im = im[:, :, ::-1]
# Zero-center by mean pixel
im[:, :, 0] -= 103.939
im[:, :, 1] -= 116.779
im[:, :, 2] -= 123.68
im = im.transpose((2, 0, 1))
im = np.expand_dims(im, axis=0)
inputs = [K.learning_phase()] + model.inputs
_convout1_f = K.function(inputs, [model.layers[31].output])
return _convout1_f([0] + [im])
def _prepare_image(I):
assert isinstance(I, np.ndarray), 'plugin error, should pass numpy array here'
assert I.ndim == 2 or I.ndim == 3 or I.ndim == 4
if I.ndim == 4: # NCHW
if I.shape[1] == 1: # N1HW
I = np.concatenate((I, I, I), 1) # N3HW
assert I.shape[1] == 3
I = make_grid(I) # 3xHxW
if I.ndim == 3 and I.shape[0] == 1: # 1xHxW
I = np.concatenate((I, I, I), 0) # 3xHxW
if I.ndim == 2: # HxW
I = np.expand_dims(I, 0) # 1xHxW
I = np.concatenate((I, I, I), 0) # 3xHxW
I = I.transpose(1, 2, 0)
return I
def validate(model):
dice_coefs = []
for image_path, label_path in zip(df_val["image"], df_val["label"]):
image = load_nifti(image_path)
label = load_nifti(label_path)
centers = [[], [], []]
for img_len, len_out, center, n_tile in zip(image.shape, args.output_shape, centers, args.n_tiles):
assert img_len < len_out * n_tile, "{} must be smaller than {} x {}".format(img_len, len_out, n_tile)
stride = int((img_len - len_out) / (n_tile - 1))
center.append(len_out / 2)
for i in range(n_tile - 2):
center.append(center[-1] + stride)
center.append(img_len - len_out / 2)
output = np.zeros((dataset["n_classes"],) + image.shape[:-1])
for x, y, z in itertools.product(*centers):
patch = crop_patch(image, [x, y, z], args.input_shape)
patch = np.expand_dims(patch, 0)
patch = xp.asarray(patch)
slices_out = [slice(center - len_out / 2, center + len_out / 2) for len_out, center in zip(args.output_shape, [x, y, z])]
slices_in = [slice((len_in - len_out) / 2, len_in - (len_in - len_out) / 2) for len_out, len_in, in zip(args.output_shape, args.input_shape)]
output[slice(None), slices_out[0], slices_out[1], slices_out[2]] += chainer.cuda.to_cpu(model(patch).data[0, slice(None), slices_in[0], slices_in[1], slices_in[2]])
y = np.argmax(output, axis=0).astype(np.int32)
dice_coefs.append(dice_coefficients(y, label, labels=range(dataset["n_classes"])))
dice_coefs = np.array(dice_coefs)
return np.mean(dice_coefs, axis=0)
def preprocess_images(images):
if images.shape[0] < 4:
# single image
x_t = images[0]
x_t = imresize(x_t, (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
s_t = np.stack((x_t, x_t, x_t, x_t), axis=2)
else:
# 4 images
xt_list = []
for i in range(images.shape[0]):
x_t = imresize(images[i], (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
xt_list.append(x_t)
s_t = np.stack((xt_list[0], xt_list[1], xt_list[2], xt_list[3]),
axis=2)
s_t = np.expand_dims(s_t, axis=0)
return s_t
############################# main ###############################
def preprocess_images(images):
if images.shape[0] < 4:
# single image
x_t = images[0]
x_t = imresize(x_t, (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
s_t = np.stack((x_t, x_t, x_t, x_t), axis=2)
else:
# 4 images
xt_list = []
for i in range(images.shape[0]):
x_t = imresize(images[i], (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
xt_list.append(x_t)
s_t = np.stack((xt_list[0], xt_list[1], xt_list[2], xt_list[3]),
axis=2)
s_t = np.expand_dims(s_t, axis=0)
return s_t
def Minibatch_Discriminator(input, num_kernels=100, dim_per_kernel=5, init=False, name='MD'):
num_inputs=df_dim*4
theta = tf.get_variable(name+"/theta",[num_inputs, num_kernels, dim_per_kernel], initializer=tf.random_normal_initializer(stddev=0.05))
log_weight_scale = tf.get_variable(name+"/lws",[num_kernels, dim_per_kernel], initializer=tf.constant_initializer(0.0))
W = tf.mul(theta, tf.expand_dims(tf.exp(log_weight_scale)/tf.sqrt(tf.reduce_sum(tf.square(theta),0)),0))
W = tf.reshape(W,[-1,num_kernels*dim_per_kernel])
x = input
x=tf.reshape(x, [batchsize,num_inputs])
activation = tf.matmul(x, W)
activation = tf.reshape(activation,[-1,num_kernels,dim_per_kernel])
abs_dif = tf.mul(tf.reduce_sum(tf.abs(tf.sub(tf.expand_dims(activation,3),tf.expand_dims(tf.transpose(activation,[1,2,0]),0))),2),
1-tf.expand_dims(tf.constant(np.eye(batchsize),dtype=np.float32),1))
f = tf.reduce_sum(tf.exp(-abs_dif),2)/tf.reduce_sum(tf.exp(-abs_dif))
print(f.get_shape())
print(input.get_shape())
return tf.concat(1,[x, f])
def do_eval(sess,model,evalX,evalY,batch_size,vocabulary_index2word_label,eval_decoder_input=None):
#ii=0
number_examples=len(evalX)
eval_loss,eval_acc,eval_counter=0.0,0.0,0
for start,end in zip(range(0,number_examples,batch_size),range(batch_size,number_examples,batch_size)):
feed_dict = {model.query: evalX[start:end],model.story:np.expand_dims(evalX[start:end],axis=1), model.dropout_keep_prob: 1}
if not FLAGS.multi_label_flag:
feed_dict[model.answer_single] = evalY[start:end]
else:
feed_dict[model.answer_multilabel] = evalY[start:end]
curr_eval_loss, logits,curr_eval_acc,pred= sess.run([model.loss_val,model.logits,model.accuracy,model.predictions],feed_dict)#curr_eval_acc--->textCNN.accuracy
eval_loss,eval_acc,eval_counter=eval_loss+curr_eval_loss,eval_acc+curr_eval_acc,eval_counter+1
#if ii<20:
#print("1.evalX[start:end]:",evalX[start:end])
#print("2.evalY[start:end]:", evalY[start:end])
#print("3.pred:",pred)
#ii=ii+1
return eval_loss/float(eval_counter),eval_acc/float(eval_counter)
#?logits????? get label using logits
def custom_loss(y_true, y_pred):
# Get prediction
pred_box_xy = tf.sigmoid(y_pred[..., :2])
pred_box_wh = y_pred[..., 2:4]
pred_box_conf = tf.sigmoid(y_pred[..., 4])
# Get ground truth
true_box_xy = y_true[..., :2]
true_box_wh = y_true[..., 2:4]
true_box_conf = y_true[..., 4]
# Determine the mask: simply the position of the ground truth boxes (the predictors)
true_mask = tf.expand_dims(y_true[..., 4], axis=-1)
# Calculate the loss. A scale can be associated with each loss, indicating how important
# the loss is. The bigger the scale, more important the loss is.
loss_xy = tf.reduce_sum(tf.square(true_box_xy - pred_box_xy) * true_mask) * 1.0
loss_wh = tf.reduce_sum(tf.square(true_box_wh - pred_box_wh) * true_mask) * 1.0
loss_conf = tf.reduce_sum(tf.square(true_box_conf - pred_box_conf)) * 1.2
loss = loss_xy + loss_wh + loss_conf
return loss
def _generate_batch(self, meta):
image = ndimage.imread(meta.image_path)
height, width, _ = meta.shape
if height > width:
scale = self._image_scale_size / width
else:
scale = self._image_scale_size / height
# TODO: the dimensions in caffe is (batch elem, channel, height, width)
resized_image = ndimage.zoom(image, (scale, scale, 1))
bboxes = np.empty((len(meta.objects), 5))
for i, obj in enumerate(meta.objects):
bboxes[i][:4] = obj['bbox']
bboxes[i][4] = obj['class_index']
return np.expand_dims(resized_image, 0), scale, bboxes
def viterbi_decode(score, transition_params):
""" Adapted from Tensorflow implementation.
Decode the highest scoring sequence of tags outside of TensorFlow.
This should only be used at test time.
Args:
score: A [seq_len, num_tags] matrix of unary potentials.
transition_params: A [num_tags, num_tags] matrix of binary potentials.
Returns:
viterbi: A [seq_len] list of integers containing the highest scoring tag
indicies.
viterbi_score: A float containing the score for the Viterbi sequence.
"""
trellis = numpy.zeros_like(score)
backpointers = numpy.zeros_like(score, dtype=numpy.int32)
trellis[0] = score[0]
for t in range(1, score.shape[0]):
v = numpy.expand_dims(trellis[t - 1], 1) + transition_params
trellis[t] = score[t] + numpy.max(v, 0)
backpointers[t] = numpy.argmax(v, 0)
viterbi = [numpy.argmax(trellis[-1])]
for bp in reversed(backpointers[1:]):
viterbi.append(bp[viterbi[-1]])
viterbi.reverse()
viterbi_score = numpy.max(trellis[-1])
return viterbi, viterbi_score
def adjust_prediction(self, probability, image):
crf = dcrf.DenseCRF(np.prod(probability.shape), 2)
# crf = dcrf.DenseCRF(np.prod(probability.shape), 1)
binary_prob = np.stack((1 - probability, probability), axis=0)
unary = unary_from_softmax(binary_prob)
# unary = unary_from_softmax(np.expand_dims(probability, axis=0))
crf.setUnaryEnergy(unary)
# per dimension scale factors
sdims = [self.sdims] * 3
smooth = create_pairwise_gaussian(sdims=sdims, shape=probability.shape)
crf.addPairwiseEnergy(smooth, compat=2)
if self.schan:
# per channel scale factors
schan = [self.schan] * 6
appearance = create_pairwise_bilateral(sdims=sdims, schan=schan, img=image, chdim=3)
crf.addPairwiseEnergy(appearance, compat=2)
result = crf.inference(self.iter)
crf_prediction = np.argmax(result, axis=0).reshape(probability.shape).astype(np.float32)
return crf_prediction
def format_img(img, C):
img_min_side = float(C.im_size)
(height,width,_) = img.shape
if width <= height:
f = img_min_side/width
new_height = int(f * height)
new_width = int(img_min_side)
else:
f = img_min_side/height
new_width = int(f * width)
new_height = int(img_min_side)
fx = width/float(new_width)
fy = height/float(new_height)
img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
img = img[:, :, (2, 1, 0)]
img = img.astype(np.float32)
img[:, :, 0] -= C.img_channel_mean[0]
img[:, :, 1] -= C.img_channel_mean[1]
img[:, :, 2] -= C.img_channel_mean[2]
img /= C.img_scaling_factor
img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
return img, fx, fy
def preprocess_batch(self, captions_label_encoded):
captions = keras_seq.pad_sequences(captions_label_encoded,
padding='post')
# Because the number of timesteps/words resulted by the model is
# maxlen(captions) + 1 (because the first "word" is the image).
captions_extended1 = keras_seq.pad_sequences(captions,
maxlen=captions.shape[-1] + 1,
padding='post')
captions_one_hot = map(self._tokenizer.sequences_to_matrix,
np.expand_dims(captions_extended1, -1))
captions_one_hot = np.array(captions_one_hot, dtype='int')
# Decrease/shift word index by 1.
# Shifting `captions_one_hot` makes the padding word
# (index=0, encoded=[1, 0, ...]) encoded all zeros ([0, 0, ...]),
# so its cross entropy loss will be zero.
captions_decreased = captions.copy()
captions_decreased[captions_decreased > 0] -= 1
captions_one_hot_shifted = captions_one_hot[:, :, 1:]
captions_input = captions_decreased
captions_output = captions_one_hot_shifted
return captions_input, captions_output
def to_pb_multibandtile(obj):
"""Converts an instance of ``Tile`` to ``ProtoMultibandTile``.
Args:
obj (:class:`~geopyspark.geotrellis.Tile`): An instance of ``Tile``.
Returns:
ProtoMultibandTile
"""
cells = obj.cells
if cells.ndim == 2:
cells = np.expand_dims(cells, 0)
band_count = cells.shape[0]
def create_tile(index):
return Tile(cells[index, :, :], obj.cell_type, obj.no_data_value)
multibandtile = ProtoMultibandTile()
multibandtile.tiles.extend([to_pb_tile(create_tile(x)) for x in range(band_count)])
return multibandtile
def getAction(self, action_space, state):
# checkpoint = tf.train.get_checkpoint_state('saved_PiNetworks_' + self.player + '/')
# if checkpoint and checkpoint.model_checkpoint_path:
# self.saver.restore(self.session, checkpoint.model_checkpoint_path)
# # print('model loaded')
self.train_phase = False
# state = np.zeros(33)
state = np.expand_dims(state, -1)
self.QValue = self.session.run(self.out, feed_dict={self.stateInput: [state], self.keep_probability: 1.0})[0]
Q_test = self.QValue * action_space
# print('Qtest ' + self.player)
# print(Q_test)
if max(Q_test) <= 0.0000001:
action_index = random.randrange(self.ACTION_NUM)
while action_space[action_index] != 1:
action_index = random.randrange(self.ACTION_NUM)
else:
action_index = np.argmax(self.QValue * action_space)
# if self.QValue[action_index] <= 0.0:
# action_index = random.randrange(self.ACTION_NUM)
# while action_space[action_index] != 1:
# action_index = random.randrange(self.ACTION_NUM)
return action_index
def format_img(img, C):
img_min_side = float(C.im_size)
(height,width,_) = img.shape
if width <= height:
f = img_min_side/width
new_height = int(f * height)
new_width = int(img_min_side)
else:
f = img_min_side/height
new_width = int(f * width)
new_height = int(img_min_side)
fx = width/float(new_width)
fy = height/float(new_height)
img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
img = img[:, :, (2, 1, 0)]
img = img.astype(np.float32)
img[:, :, 0] -= C.img_channel_mean[0]
img[:, :, 1] -= C.img_channel_mean[1]
img[:, :, 2] -= C.img_channel_mean[2]
img /= C.img_scaling_factor
img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
return img, fx, fy
def tensor_swirl(image, center=None, strength=1, radius=100, rotation=0, cval=0.0, **kwargs):
# **kwargs is for unsupported options (ignored)
cval = tf.fill(K.shape(image)[0:1], cval)
shape = K.int_shape(image)[1:3]
if center is None:
center = np.array(shape) / 2
ys = np.expand_dims(np.repeat(np.arange(shape[0]), shape[1]),-1)
xs = np.expand_dims(np.tile (np.arange(shape[1]), shape[0]),-1)
map_xs, map_ys = swirl_mapping(xs, ys, center, rotation, strength, radius)
mapping = np.zeros((*shape, *shape))
for map_x, map_y, x, y in zip(map_xs, map_ys, xs, ys):
results = tensor_linear_interpolation(image, map_x, map_y, cval)
for _y, _x, w in results:
# mapping[int(y),int(x),int(_y),int(_x),] = w
mapping[int(_y),int(_x),int(y),int(x),] = w
results = tf.tensordot(image, K.variable(mapping), [[1,2],[0,1]])
# results = K.reshape(results, K.shape(image))
return results
def imgread(img_path, scale = 4):
img = scipy.misc.imread(img_path)
img = img /256.0
h,w,c = img.shape
tmp1 = h % scale
new_h = h + scale - tmp1
tmp2 = w % scale
new_w = w +scale-tmp2
img = np.pad(img, ((0,scale-tmp1), (0, scale-tmp2),(0,0)), mode = 'reflect')
if scale != None:
img = np.expand_dims(img,0)
img = tf.convert_to_tensor(img)
lr_w = new_w / scale
lr_h = new_h /scale
img = tf.cast(img, tf.float32)
img_lr = tf.image.resize_images(img, [lr_h, lr_w])
img_lr = tf.cast(img_lr,tf.float32)
return img_lr, img
return img
def imgread(img_path, scale = 4):
img = scipy.misc.imread(img_path)
#img = scipy.misc.imresize(img, (128, 128))
img = img /256.0
h,w,c = img.shape
new_h = pow(2, int(math.log(h, 2))+1)
tmp1 = new_h - h
new_w = pow(2, int(math.log(w, 2))+1)
tmp2 = new_w - w
img = np.pad(img, ((0,tmp1), (0, tmp2),(0,0)), mode = 'constant')
if scale != None:
img = np.expand_dims(img,0)
img = tf.convert_to_tensor(img)
lr_w = new_w / scale
lr_h = new_h /scale
img = tf.cast(img, tf.float32)
img_lr = tf.image.resize_images(img, [lr_h, lr_w])
img_lr = tf.cast(img_lr,tf.float32)
return img_lr, img
return img
def preprocess_label(label):
"""
load label image, keep a single channel (all three should be the same)
:param label:
:return:
"""
# TODO: Populate with actual logic and move away from here into a dedicated class (like ImageDataGenerator)
# target = 'pillow' if mode == 'pillow' else 'numpy'
# # lbl = resize(lbl, target_h, target_w, mode=mode, target_type='numpy', keep_aspect_ratio=keep_aspect_ratio)
# if mode == 'pillow':
# # lbl = np.expand_dims(lbl[:, :, 0], axis=2)
# assert np.all(lbl[:, :, 0] == lbl[:, :, 1]) and np.all(lbl[:, :, 0] == lbl[:, :, 2])
# lbl = lbl[:, :, 0].astype(np.uint8)
# array2d = mapper[lbl]
# onehot_lbl = to_categorical(array2d, num_classes=nc)
# return onehot_lbl
return label
def dist_info_sym(self, obs_var, latent_var=None): # this is ment to be for one path!
# now this is not doing anything! And for computing the dist_info_vars of npo_snn_rewardMI it doesn't work
if latent_var is None:
latent_var1 = theano.shared(np.expand_dims(self.latent_fix, axis=0)) # new fix to avoid putting the latent as an input: just take the one fixed!
latent_var = TT.tile(latent_var1, [obs_var.shape[0], 1])
# generate the generalized input (append latents to obs.)
if self.bilinear_integration:
extended_obs_var = TT.concatenate([obs_var, latent_var,
TT.flatten(obs_var[:, :, np.newaxis] * latent_var[:, np.newaxis, :],
outdim=2)]
, axis=1)
else:
extended_obs_var = TT.concatenate([obs_var, latent_var], axis=1)
mean_var, log_std_var = L.get_output([self._l_mean, self._l_log_std], extended_obs_var)
if self.min_std is not None:
log_std_var = TT.maximum(log_std_var, np.log(self.min_std))
return dict(mean=mean_var, log_std=log_std_var)
def variable(value, dtype=_FLOATX, name=None):
'''Instantiates a tensor.
# Arguments
value: numpy array, initial value of the tensor.
dtype: tensor type.
name: optional name string for the tensor.
# Returns
Tensor variable instance.
'''
if hasattr(value, 'tocoo'):
sparse_coo = value.tocoo()
indices = np.concatenate((np.expand_dims(sparse_coo.row, 1),
np.expand_dims(sparse_coo.col, 1)), 1)
# SparseTensor doesn't need initialization
v = tf.SparseTensor(indices=indices, values=sparse_coo.data, shape=sparse_coo.shape)
v._dims = len(sparse_coo.shape)
return v
v = tf.Variable(value, dtype=_convert_string_dtype(dtype), name=name)
return v
def test_shape_operations(self):
# concatenate
xval = np.random.random((4, 3))
xth = KTH.variable(xval)
xtf = KTF.variable(xval)
yval = np.random.random((4, 2))
yth = KTH.variable(yval)
ytf = KTF.variable(yval)
zth = KTH.eval(KTH.concatenate([xth, yth], axis=-1))
ztf = KTF.eval(KTF.concatenate([xtf, ytf], axis=-1))
assert zth.shape == ztf.shape
assert_allclose(zth, ztf, atol=1e-05)
check_single_tensor_operation('reshape', (4, 2), shape=(8, 1))
check_single_tensor_operation('permute_dimensions', (4, 2, 3),
pattern=(2, 0, 1))
check_single_tensor_operation('repeat', (4, 1), n=3)
check_single_tensor_operation('flatten', (4, 1))
check_single_tensor_operation('expand_dims', (4, 3), dim=-1)
check_single_tensor_operation('expand_dims', (4, 3, 2), dim=1)
check_single_tensor_operation('squeeze', (4, 3, 1), axis=2)
check_single_tensor_operation('squeeze', (4, 1, 1), axis=1)
check_composed_tensor_operations('reshape', {'shape': (4, 3, 1, 1)},
'squeeze', {'axis': 2},
(4, 3, 1, 1))
def render(self, mode='human', close=False):#*args, **kwargs):
env = self.conopt_env
if close:
if 'viewer' in env.__dict__:
env.viewer.close()
del env.viewer
else:
img = env.world.model.render(np.expand_dims(env.x, 0))[0]
if mode == 'human':
#import cv2
#img = cv2.resize(img, (50, 50))
if not 'viewer' in env.__dict__:
from gym.envs.classic_control.rendering import SimpleImageViewer
env.viewer = SimpleImageViewer()
env.viewer.imshow(img)
return img
else:
return img
def readimg(img_path):
img = misc.imread(img_path, mode='RGB')
img = misc.imresize(img, (160, 160))
img = facenet.prewhiten(img)
img = np.expand_dims(img, axis=0)
return img
def get_embedding(img_path):
img = misc.imread(img_path, mode='RGB')
# judge alignment
aligned = align.align(160, img, [0, 0, img.shape[1], img.shape[0]], landmarkIndices=landmarkIndices)
img = facenet.prewhiten(img)
img = np.expand_dims(img, axis=0)
aligned = facenet.prewhiten(aligned)
aligned = np.expand_dims(aligned, axis=0)
# Run forward pass to calculate embeddings
feed_dict = {images_placeholder: img, phase_train_placeholder: False}
emb = sess.run(embeddings, feed_dict=feed_dict)
# Run forward pass to calculate embeddings
feed_dict_aligned = {images_placeholder: aligned, phase_train_placeholder: False}
emb_aligned = sess.run(embeddings, feed_dict=feed_dict_aligned)
return emb.ravel(), emb_aligned.ravel()
# # for test
# import os
# from time import time
# def main(dir_path):
# img_all = os.listdir(dir_path)
# for f in img_all:
# start = time()
# embedding_result = get_embedding(os.path.join(dir_path, f))
# print time() - start
# print embedding_result
#
# main('./data')
def zscore(a, axis=0, ddof=0):
a = np.asanyarray(a)
mns = a.mean(axis=axis)
sstd = a.std(axis=axis, ddof=ddof)
if axis and mns.ndim < a.ndim:
res = (((a - np.expand_dims(mns, axis=axis)) /
np.expand_dims(sstd,axis=axis)))
else:
res = (a - mns) / sstd
return np.nan_to_num(res)
def is_catastrophe(self, obs):
X = np.expand_dims(obs, axis=0)
X = np.reshape(X, [X.shape[0], -1])
return self.classifier.predict(X)[0]
def load_features_episode(self, episode):
features = {}
observations = [frame.observation for frame in episode.frames if frame.has_action()]
features['observation'] = np.concatenate(
[np.expand_dims(observation, axis=0) for observation in observations], axis=0)
images = [frame.image for frame in episode.frames if frame.has_action()]
features['image'] = np.concatenate(
[process_image(image, self.hparams) for image in images], axis=0)
actions = [frame.get_proposed_action() for frame in episode.frames if frame.has_action()]
features['action'] = np.expand_dims(np.array(actions), axis=1)
features['index'] = np.array(
[i for i, frame in enumerate(episode.frames) if frame.has_action()])
return features
def load_features_incident_records(self, incident_records):
original_images = []
actions = []
for block_record_file in incident_records:
with open(block_record_file, 'rb') as f:
block_record = pickle.load(f)
original_images.append(block_record.obs)
actions.append(block_record.action)
features = {}
features['image'] = np.concatenate(
[process_image(image, self.hparams) for image in original_images], axis=0)
features['action'] = np.expand_dims(np.array(actions), axis=1)
return features, original_images
def process_image(image, hparams):
desired_shape = hparams.image_shape
if hparams.image_crop_region is not None:
image = image[hparams.image_crop_region[0][0]:hparams.image_crop_region[0][1],
hparams.image_crop_region[1][0]:hparams.image_crop_region[1][1]]
if not tuple(image.shape) == tuple(desired_shape):
image = cv2.resize(image, (desired_shape[1], desired_shape[0]))
assert tuple(image.shape) == tuple(desired_shape), "{}, {}".format(image.shape, desired_shape)
return np.expand_dims(image.astype(np.float32) / 256.0, axis=0)
def label(self, features, episode):
labels = np.array(
[self._has_label(frame) for frame in episode.frames if frame.has_action()])
actions = [
frame.get_action(self.action_type) for frame in episode.frames if frame.has_action()
]
features['action'] = np.expand_dims(np.array(actions), axis=1)
return features, labels
def load_features_episode(self, episode):
features = {}
observations = [frame.observation for frame in episode.frames if frame.has_action()]
features['observation'] = np.concatenate(
[np.expand_dims(observation, axis=0) for observation in observations], axis=0)
images = [frame.image for frame in episode.frames if frame.has_action()]
features['image'] = np.concatenate(
[process_image(image, self.hparams) for image in images], axis=0)
actions = [frame.get_proposed_action() for frame in episode.frames if frame.has_action()]
features['action'] = np.expand_dims(np.array(actions), axis=1)
features['index'] = np.array(
[i for i, frame in enumerate(episode.frames) if frame.has_action()])
return features
def load_features_incident_records(self, incident_records):
original_images = []
actions = []
for block_record_file in incident_records:
with open(block_record_file, 'rb') as f:
block_record = pickle.load(f)
original_images.append(block_record.obs)
actions.append(block_record.action)
features = {}
features['image'] = np.concatenate(
[process_image(image, self.hparams) for image in original_images], axis=0)
features['action'] = np.expand_dims(np.array(actions), axis=1)
return features, original_images
def process_image(image, hparams):
desired_shape = hparams.image_shape
if hparams.image_crop_region is not None:
image = image[hparams.image_crop_region[0][0]:hparams.image_crop_region[0][1],
hparams.image_crop_region[1][0]:hparams.image_crop_region[1][1]]
if not tuple(image.shape) == tuple(desired_shape):
image = cv2.resize(image, (desired_shape[1], desired_shape[0]))
assert tuple(image.shape) == tuple(desired_shape), "{}, {}".format(image.shape, desired_shape)
return np.expand_dims(image.astype(np.float32) / 256.0, axis=0)
def label(self, features, episode):
labels = np.array(
[self._has_label(frame) for frame in episode.frames if frame.has_action()])
actions = [
frame.get_action(self.action_type) for frame in episode.frames if frame.has_action()
]
features['action'] = np.expand_dims(np.array(actions), axis=1)
return features, labels
def is_catastrophe(self, obs):
X = np.expand_dims(obs, axis=0)
X = np.reshape(X, [X.shape[0], -1])
return self.classifier.predict(X)[0]
def load_features_incident_records(self, incident_records):
original_images = []
actions = []
for block_record_file in incident_records:
with open(block_record_file, 'rb') as f:
block_record = pickle.load(f)
original_images.append(block_record.obs)
actions.append(block_record.action)
features = {}
features['image'] = np.concatenate(
[process_image(image, self.hparams) for image in original_images], axis=0)
features['action'] = np.expand_dims(np.array(actions), axis=1)
return features, original_images
def process_image(image, hparams):
desired_shape = hparams.image_shape
if hparams.image_crop_region is not None:
image = image[hparams.image_crop_region[0][0]:hparams.image_crop_region[0][1],
hparams.image_crop_region[1][0]:hparams.image_crop_region[1][1]]
if not tuple(image.shape) == tuple(desired_shape):
image = cv2.resize(image, (desired_shape[1], desired_shape[0]))
assert tuple(image.shape) == tuple(desired_shape), "{}, {}".format(image.shape, desired_shape)
return np.expand_dims(image.astype(np.float32) / 256.0, axis=0)
def label(self, features, episode):
labels = np.array(
[self._has_label(frame) for frame in episode.frames if frame.has_action()])
actions = [
frame.get_action(self.action_type) for frame in episode.frames if frame.has_action()
]
features['action'] = np.expand_dims(np.array(actions), axis=1)
return features, labels
def is_catastrophe(self, obs):
X = np.expand_dims(obs, axis=0)
X = np.reshape(X, [X.shape[0], -1])
return self.classifier.predict(X)[0]
def load_features_episode(self, episode):
features = {}
observations = [frame.observation for frame in episode.frames if frame.has_action()]
features['observation'] = np.concatenate(
[np.expand_dims(observation, axis=0) for observation in observations], axis=0)
images = [frame.image for frame in episode.frames if frame.has_action()]
features['image'] = np.concatenate(
[process_image(image, self.hparams) for image in images], axis=0)
actions = [frame.get_proposed_action() for frame in episode.frames if frame.has_action()]
features['action'] = np.expand_dims(np.array(actions), axis=1)
features['index'] = np.array(
[i for i, frame in enumerate(episode.frames) if frame.has_action()])
return features