def __call__(self, x): tensors = [] for in_tensor in x: shape = K.int_shape(in_tensor) if shape[1] == self.output_dim: tensor = Conv2D(self.cur_channels, strides=1, **self.conv_param)(in_tensor) tensors.append(tensor) if shape[1] > self.output_dim: tensor = Conv2D(self.cur_channels, strides=2, **self.conv_param)(in_tensor) tensors.append(tensor) if shape[1] < self.output_dim: tensor = Lambda(lambda x: K.resize_images( x, 2, 2, 'channels_last'))(in_tensor) tensors.append(tensor) if len(tensors) > 1: tensor = Add()(tensors) else: tensor = tensors[0] tensor = BatchNormalization()(tensor) tensor = Activation('relu')(tensor) return tensor
def resize_image(inp, s, data_format): return Lambda(lambda x: K.resize_images(x, height_factor=s[0], width_factor=s[1], data_format=data_format, interpolation='bilinear'))(inp)
def __call__(self, shape, dtype="float32"): """ Call function for the ICNR initializer. Parameters ---------- shape: tuple or list The required resized shape for the output tensor dtype: str The data type for the tensor Returns ------- tensor The modified kernel weights """ shape = list(shape) if self.scale == 1: return self.initializer(shape) new_shape = shape[:3] + [shape[3] // (self.scale**2)] if isinstance(self.initializer, dict): self.initializer = initializers.deserialize(self.initializer) var_x = self.initializer(new_shape, dtype) var_x = K.permute_dimensions(var_x, [2, 0, 1, 3]) var_x = K.resize_images(var_x, self.scale, self.scale, "channels_last", interpolation="nearest") var_x = self._space_to_depth(var_x) var_x = K.permute_dimensions(var_x, [1, 2, 0, 3]) logger.debug("Output shape: %s", var_x.shape) return var_x
def resize_image(self, input, factors): return Lambda(lambda x: K.resize_images(x=x, height_factor=factors[0], width_factor=factors[1], data_format='channels_last', interpolation='bilinear'))( input)
def call(self, inputs, **kwargs): # pylint:disable=unused-argument """ Call the upsample layer Parameters ---------- inputs: tensor Input tensor, or list/tuple of input tensors kwargs: dict Additional keyword arguments. Unused Returns ------- tensor A tensor or list/tuple of tensors """ if isinstance(self.size, int): retval = K.resize_images(inputs, self.size, self.size, "channels_last", interpolation=self.interpolation) else: # Arbitrary resizing size = int(round(K.int_shape(inputs)[1] * self.size)) if get_backend() != "amd": retval = tf.image.resize(inputs, (size, size), method=self.interpolation) else: raise NotImplementedError return retval
def resize(input): (bs, h, w, num_c) = K.int_shape(input) map = K.resize_images( input, 224/7, 224/7, "channels_last" ) # print("map shape", K.int_shape(map)) # normalize heatmap # min = K.expand_dims(K.expand_dims(K.min(map, axis=(1,2)))) # min = K.repeat_elements(min, rep=h0, axis=1) # min = K.repeat_elements(min, rep=w0, axis= 2) # max = K.expand_dims(K.expand_dims(K.max(map, axis=(1,2)))) # max = K.repeat_elements(max, rep=h0, axis=1) # max = K.repeat_elements(max, rep=w0, axis= 2) # map = layers.BatchNormalization()(map) min = K.min(map) map = map - min sum = K.sum(map, axis=(1,2,3)) sum = K.expand_dims(K.expand_dims(K.expand_dims(sum))) sum = K.repeat_elements(sum, 224, axis=1) sum = K.repeat_elements(sum, 224, axis=2) # sum = K.sum(map) map = 224*map/sum # force the sum of attention to be a constant return map
def max_depool(self, in_layer=None, depth_factor=2, height_factor=2, width_factor=2): if in_layer == None: in_layer = self.layer #if 1: # alt with deconv #lo, li = self.deconvolutional_layer(1, [1,1], None, stride=[2,2], name="g_D1", reuse=reuse, batch_norm=use_batch_norm, train=train) #return lo ''' if len(self.layer.get_shape()) == 4: outWidth = in_layer.get_shape()[2] * window_stride[0] + window_size[0] - window_stride[0] outHeight = in_layer.get_shape()[1] * window_stride[1] + window_size[1] - window_stride[1] self.layer = tf.image.resize_images(in_layer, [int(outHeight), int(outWidth)], 1) #1 = ResizeMethod.NEAREST_NEIGHBOR print("Max Depool {}: {}".format(window_size, self.layer.get_shape())) ''' if len(self.layer.get_shape()) == 4: #self.layer = tf.contrib.keras.backend.resize_images(self.layer, height_factor, width_factor, 'channels_last') self.layer = kb.resize_images(self.layer, height_factor, width_factor, 'channels_last') print("Max Depool : {}".format(self.layer.get_shape())) if len(self.layer.get_shape()) == 5: #self.layer = tf.contrib.keras.backend.resize_volumes(self.layer, depth_factor, height_factor, width_factor, 'channels_last') self.layer = kb.resize_volumes(self.layer, depth_factor, height_factor, width_factor, 'channels_last') print("Max Depool : {}".format(self.layer.get_shape())) return self.layer
def get_model(): img_input = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 1)) x = img_input print(x.get_shape()) x = Dense(32, activation='relu')(x) x = Conv2D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu')(x) x = BatchNormalization()(x) x = UpSampling2D(size=2)(x) x = MaxPooling2D(pool_size=(2, 2))(x) x = Flatten()(x) x = Dropout(0.25)(x) x = Reshape(target_shape=(int(conv_shape[1]), int(conv_shape[2]*conv_shape[3])))(x) model.add(LSTM(LSTM_UNITS, input_shape=(SEN_LEN, len(all_chars)), return_sequences=True)) model.add(LSTM(LSTM_UNITS, input_shape=(SEN_LEN, len(all_chars)), return_sequences=False)) # 新的写法 model.add(Bidirectional(LSTM(128, dropout=0.2, recurrent_dropout=0.2))) # 废弃的写法 # gru_1 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal')(x) # gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal')(x) # # keras.layers.add # gru1_merged = add([gru_1, gru_1b]) x = K.reshape(x, (-1, AB_PAIRS)) x = K.softmax(x) x = K.resize_images(x, IMG_HEIGHT / curr_height, IMG_WIDTH / curr_width, data_format="channels_last") x = Lambda(lambda z: reshape(z), output_shape=(IMG_HEIGHT, IMG_WIDTH, AB_PAIRS))(x) model = Model(inputs=img_input, outputs=x) model.compile(loss='categorical_crossentropy', optimizer='Adadelta', metrics=['accuracy'])
def call(self, x): h, w = self.shape[1:3] for l in levels: pool = K.pool2d(x, pool_size=(h // l, w // l), strides=(h // l, w // l), padding='valid', data_format=None, pool_mode='max') pool = K.resize_images(x, h * l, w * l, data_format='channels_last') x += pool return x
def _resize_nearest_neighbour(self, input_tensor, size): """ Resize a tensor using nearest neighbor interpolation. Notes ----- Tensorflow has a bug that resizes the image incorrectly if :attr:`align_corners` is not set to ``True``. Keras Backend does not set this flag, so we explicitly call the Tensorflow operation for non-amd backends. Parameters ---------- input_tensor: tensor The tensor to be resized tuple: int The (`h`, `w`) that the tensor should be resized to (used for non-amd backends only) Returns ------- tensor The input tensor resized to the given size """ if get_backend() == "amd": retval = K.resize_images(input_tensor, self.scale, self.scale, "channels_last", interpolation="nearest") else: retval = tf.image.resize_nearest_neighbor(input_tensor, size=size, align_corners=True) logger.debug("Input Tensor: %s, Output Tensor: %s", input_tensor, retval) return retval
def _backward_pass(self, X, target_layer, d_switch, feat_map): # Run deconv/maxunpooling until input pixel space layer_index = self.lnames.index(target_layer) # Get the output of the target_layer of interest out = self[target_layer].output if (str(type(out)) != "<type 'list'>"): out = [out] layer_output = K.function([self[self.lnames[0]].input], out) X_outl = layer_output([X]) # Special case for the starting layer where we may want # to switchoff somes maps/ activations print("Deconvolving %s..." % target_layer) #if "maxpooling2d" in target_layer: if "pool" in target_layer: #X_maxunp = K.pool.max_pool_2d_same_size( X_maxunp = pool.max_pool_2d_same_size(self[target_layer].input, self[target_layer].pool_size) unpool_func = K.function([self[self.lnames[0]].input], X_maxunp) X_outl = unpool_func([X]) if feat_map is not None: for i in range(X_outl.shape[1]): if i != feat_map: X_outl[:, i, :, :] = 0 for i in range(X_outl.shape[0]): iw, ih = np.unravel_index( X_outl[i, feat_map, :, :].argmax(), X_outl[i, feat_map, :, :].shape) m = np.max(X_outl[i, feat_map, :, :]) X_outl[i, feat_map, :, :] = 0 X_outl[i, feat_map, iw, ih] = m #elif "conv2d" in target_layer: elif "conv" in target_layer: X_outl = self._deconv(X_outl, target_layer, d_switch, feat_map=feat_map) else: raise ValueError( "Invalid layer name: %s \n Can only handle maxpool and conv" % target_layer) # Iterate over layers (deepest to shallowest) for lname in self.lnames[:layer_index][::-1]: print("Deconvolving %s..." % lname) # Unpool, Deconv or do nothing if "maxpooling2d" in lname: p1, p2 = self[lname].pool_size uppool = K.function([self.x], K.resize_images(self.x, p1, p2, "th")) X_outl = uppool([X_outl]) elif "conv2d" in lname: X_outl = self._deconv(X_outl, lname, d_switch) elif "padding" in lname: pass else: raise ValueError( "Invalid layer name: %s \n Can only handle maxpool and conv" % lname) return X_outl
def build_image_resizer(): input_layer = Input(shape=(64, 64, 3)) resized_images = Lambda(lambda x: K.resize_images(x, height_factor=3, width_factor=3, data_format='channels_last'))(input_layer) model = Model(inputs=[input_layer], outputs=[resized_images]) return model
def call(self, x, mask=None): output = K.resize_images(x, self.size[0], self.size[1], self.dim_ordering) f = K.gradients(K.sum(self._master_layer.output), self._master_layer.input) * output return f
def call(self, inputs): (x, scale, bias) = inputs mean = K.mean(x, axis=self.axis, keepdims=True) std = K.std(x, axis=self.axis, keepdims=True) + self.epsilon scale = K.resize_images(scale, self.scale_ratio, self.scale_ratio, K.image_data_format(), interpolation=self.interpolation) bias = K.resize_images(bias, self.scale_ratio, self.scale_ratio, K.image_data_format(), interpolation=self.interpolation) return (x - mean) / std * scale + bias
def call(self, x, mask=None): # TODO: implement for dim_ordering='tf' img = K.resize_images(x, self.size[0], self.size[1], self.dim_ordering) padded = T.zeros( (img.shape[0], img.shape[1], self.shape[0], self.shape[1])) padded = T.set_subtensor(padded[:, :, :img.shape[2], :img.shape[3]], img) return T.switch(self.ind, padded, T.zeros_like(padded))
def call(self, inputs): return backend.resize_images( inputs, self.size[0], self.size[1], self.data_format, interpolation=self.interpolation, )
def call(self, x, mask=None): resize_image = K.resize_images(K.expand_dims(K.expand_dims(1 + self.kernel, -1), 0), self.downsampling_factor, self.downsampling_factor, data_format=K.image_data_format(), interpolation='bilinear') # resize_image = K.concatenate([K.zeros_like(resize_image[:, :1, :, :]), resize_image], axis=1) # resize_image = K.concatenate([resize_image, K.zeros_like(resize_image[:, :1, :, :])], axis=1) # resize_image = K.concatenate([K.zeros_like(resize_image[:, :, :1, :]), resize_image], axis=2) # resize_image = K.concatenate([resize_image, K.zeros_like(resize_image[:, :, :1, :])], axis=2) output = x * resize_image return output
def deconv(data, filters, s=1): output = Lambda(lambda x: K.resize_images(x, 2, 2, "channels_last"))(data) #output = Lambda(lambda x:tf.image.resize_image_with_pad(x,h,h))(data) output = Conv2D(filters, (3, 3), strides=s, activation='relu', padding='same')(output) return output
def _op_when_equal(self, inputs, pool_size, strides, padding, data_format): self.expected_out_shape = K.int_shape(inputs[1]) self.size = self.strides mask = K.pool2d(inputs[1], pool_size, strides, padding, data_format, pool_mode='max') mask = K.resize_images(mask, self.size[0], self.size[1], self.data_format) adjusted_ref = self.adjust_reference_tensor(inputs[1], case='equal') mask = K.greater_equal(adjusted_ref, mask) mask = self._adjust_size(mask) mask = K.cast(mask, dtype='float32') upsampled_input = self._adjust_size( K.resize_images(inputs[0], self.size[0], self.size[1], self.data_format)) return upsampled_input * mask
def reshape_softmax(x): x = K.permute_dimensions(x, [0, 2, 3, 1]) # last dimension in number of filters x = K.reshape(x, (batch_size * current_h * current_w, nb_classes)) x = K.softmax(x) # Add a zero column so that x has the same dimension as the target (313 classes + 1 weight) xc = K.zeros((batch_size * current_h * current_w, 1)) x = K.concatenate([x, xc], axis=1) # Reshape back to (batch_size, h, w, nb_classes + 1) to satisfy keras' shape checks x = K.reshape(x, (batch_size, current_h, current_w, nb_classes + 1)) x = K.resize_images(x, h / current_h, w / current_w, "tf") # x = K.permute_dimensions(x, [0, 3, 1, 2]) return x
def reshape_softmax(x): x = K.permute_dimensions( x, [0, 2, 3, 1]) # last dimension in number of filters x = K.reshape(x, (batch_size * current_h * current_w, nb_classes)) x = K.softmax(x) # Add a zero column so that x has the same dimension as the target (313 classes + 1 weight) xc = K.zeros((batch_size * current_h * current_w, 1)) x = K.concatenate([x, xc], axis=1) # Reshape back to (batch_size, h, w, nb_classes + 1) to satisfy keras' shape checks x = K.reshape(x, (batch_size, current_h, current_w, nb_classes + 1)) x = K.resize_images(x, h / current_h, w / current_w, "tf") # x = K.permute_dimensions(x, [0, 3, 1, 2]) return x
def call(self, input_tensor, training=None): input_transposed = tf.transpose(input_tensor, [4, 0, 1, 2, 3, 5]) input_shape = K.shape(input_transposed) input_tensor_reshaped = K.reshape(input_transposed, [ input_shape[1] * input_shape[0], self.input_height, self.input_width, self.input_depth, self.input_num_atoms]) input_tensor_reshaped.set_shape((None, self.input_height, self.input_width, self.input_depth, self.input_num_atoms)) if self.upsamp_type == 'resize': # added 1 more self.scaling upsamp = K.resize_images(input_tensor_reshaped, self.scaling, self.scaling, self.scaling, 'channels_last') outputs = K.conv3d(upsamp, kernel=self.W, strides=(1, 1, 1), padding=self.padding, data_format='channels_last') elif self.upsamp_type == 'subpix': conv = K.conv3d(input_tensor_reshaped, kernel=self.W, strides=(1, 1, 1), padding='same', data_format='channels_last') outputs = tf.depth_to_space(conv, self.scaling) else: batch_size = input_shape[1] * input_shape[0] # Infer the dynamic output shape: out_height = deconv_length(self.input_height, self.scaling, self.kernel_size, self.padding) out_width = deconv_length(self.input_width, self.scaling, self.kernel_size, self.padding) out_depth = deconv_length(self.input_depth, self.scaling, self.kernel_size, self.padding) output_shape = (batch_size, out_height, out_width, out_depth, self.num_capsule * self.num_atoms) outputs = K.conv3d_transpose(input_tensor_reshaped, self.W, output_shape, (self.scaling, self.scaling, self.scaling), padding=self.padding, data_format='channels_last') votes_shape = K.shape(outputs) _, conv_height, conv_width, conv_depth, _ = outputs.get_shape() votes = K.reshape(outputs, [input_shape[2], input_shape[1], input_shape[0], votes_shape[1], votes_shape[2], self.num_capsule, self.num_atoms]) votes.set_shape((None, self.input_num_capsule, conv_height.value, conv_width.value, conv_depth.value, self.num_capsule, self.num_atoms)) logit_shape = K.stack([ input_shape[1], input_shape[0], votes_shape[1], votes_shape[2], votes_shape[3], self.num_capsule]) biases_replicated = K.tile(self.b, [votes_shape[1], votes_shape[2], votes_shape[3], 1, 1]) activations = update_routing( votes=votes, biases=biases_replicated, logit_shape=logit_shape, num_dims=7, input_dim=self.input_num_capsule, output_dim=self.num_capsule, num_routing=self.routings) return activations
def reshape_softmax(x): shape = K.shape(x) x = K.reshape(x, (shape[0] * shape[1] * shape[2], 400)) x = K.softmax(x) # Add a zero column so that x has the same dimension as the target (313 classes + 1 weight) xc = K.zeros((b_size * 56 * 56, 1)) x = K.concatenate([x, xc], axis=-1) # Reshape back to (batch_size, h, w, nb_classes + 1) to satisfy keras' shape checks x = K.reshape(x, (shape[0], shape[1], shape[2], 400 + 1)) x = K.resize_images(x, 224 // 56, 224 // 56, "channels_last") return x
def normalize_pool_map(score_map, pool_size=(2, 2), strides=(2, 2), temperature=1): # compute safe softmax by pool operation padding = 'same' input_shape = K.int_shape(score_map) pooled_max_map = K.pool2d(score_map, pool_size=pool_size, strides=strides, pool_mode='max', padding=padding) max_map = K.resize_images(pooled_max_map, pool_size[0], pool_size[1], data_format='channels_last') max_map = max_map[:, :input_shape[1], :input_shape[2], :] max_map = K.stop_gradient(max_map) score_map = score_map - max_map score_map = tf.exp(score_map / temperature) pooled_score_map = K.pool2d(score_map, pool_size=pool_size, strides=strides, pool_mode='avg', padding=padding) avg_score_map = K.resize_images(pooled_score_map, pool_size[0], pool_size[1], data_format='channels_last') # fit caps columns to feature map avg_score_map = avg_score_map[:, :input_shape[1], :input_shape[2], :] return score_map / (avg_score_map + K.epsilon())
def _backward_pass(self, X, target_layer, d_switch, feat_map): # Run deconv/maxunpooling until input pixel space layer_index = self.lnames.index(target_layer) # Get the output of the target_layer of interest layer_output = K.function( [self[self.lnames[0]].input], self[target_layer].output) X_outl = layer_output([X]) # Special case for the starting layer where we may want # to switchoff somes maps/ activations print "Deconvolving %s..." % target_layer if "maxpooling2d" in target_layer: X_maxunp = K.pool.max_pool_2d_same_size( self[target_layer].input, self[target_layer].pool_size) unpool_func = K.function([self[self.lnames[0]].input], X_maxunp) X_outl = unpool_func([X]) if feat_map is not None: for i in range(X_outl.shape[1]): if i != feat_map: X_outl[:, i, :, :] = 0 for i in range(X_outl.shape[0]): iw, ih = np.unravel_index( X_outl[i, feat_map, :, :].argmax(), X_outl[i, feat_map, :, :].shape) m = np.max(X_outl[i, feat_map, :, :]) X_outl[i, feat_map, :, :] = 0 X_outl[i, feat_map, iw, ih] = m elif "convolution2d" in target_layer: X_outl = self._deconv(X_outl, target_layer, d_switch, feat_map=feat_map) else: raise ValueError( "Invalid layer name: %s \n Can only handle maxpool and conv" % target_layer) # Iterate over layers (deepest to shallowest) for lname in self.lnames[:layer_index][::-1]: print "Deconvolving %s..." % lname # Unpool, Deconv or do nothing if "maxpooling2d" in lname: p1, p2 = self[lname].pool_size uppool = K.function( [self.x], K.resize_images(self.x, p1, p2, "th")) X_outl = uppool([X_outl]) elif "convolution2d" in lname: X_outl = self._deconv(X_outl, lname, d_switch) elif "padding" in lname: pass else: raise ValueError( "Invalid layer name: %s \n Can only handle maxpool and conv" % lname) return X_outl
def call(self, x, mask=None): x = K.sum(x, axis=-1, keepdims=True) x = K.resize_images(x, self.nb_row, self.nb_col, self.dim_ordering) conv_out = self.deconv2d(x=x, kernel=self.W, dim_ordering=self.dim_ordering, filter_shape=self.W_shape) if self.dim_ordering == 'th': output = conv_out + K.reshape(self.b, (self.nb_filter, 1, 1)) elif self.dim_ordering == 'tf': output = conv_out + K.reshape(self.b, (self.nb_filter, 1, 1)) # output = conv_out + self.b else: raise Exception('Invalid dim_ordering: ' + self.dim_ordering) output = self.activation(output) # return output results_shape = output.shape return output.reshape((1, results_shape[0], results_shape[1], results_shape[2]))
def call(self, inputs, **kwargs): source, target = inputs source_shape = K.shape(source) target_shape = K.shape(target) if K.image_data_format() == 'channels_last': source_height, source_width = source_shape[1:3] target_height, target_width = target_shape[1:3] else: source_height, source_width = source_shape[2:4] target_height, target_width = target_shape[2:4] return K.resize_images(source, target_height / source_height, target_width / source_width, K.image_data_format())
def resize_image(inp, s, data_format): try: return Lambda(lambda x: K.resize_images(x, height_factor=s[0], width_factor=s[1], data_format=data_format, interpolation='bilinear'))(inp) except Exception as e: # if keras is old , then rely on the tf function ... sorry theono/cntk users . assert data_format == 'channels_last' assert IMAGE_ORDERING == 'channels_last' return Lambda(lambda x: tf.image.resize_images(x, (K.int_shape(x)[ 1] * s[0], K.int_shape(x)[2] * s[1])))(inp)
def build_fr_combined_network(encoder, generator, fr_model): input_image = Input(shape=(64, 64, 3)) input_label = Input(shape=(6,)) latent0 = encoder(input_image) gen_images = generator([latent0, input_label]) fr_model.trainable = False resized_images = Lambda(lambda x: K.resize_images(gen_images, height_factor=2, width_factor=2, data_format='channels_last'))(gen_images) embeddings = fr_model(resized_images) model = Model(inputs=[input_image, input_label], outputs=[embeddings]) return model
def get_cam_img(model, X, label, cam_conv_layer_name, ratio=1): """Get class map image. # Arguments: model: Trained CAM model X: test image array label: which label you want to visualize cam_conv_layer_name: the name of new added conv layer ratio: upsampling ratio """ inc = model.input # The activation of conv before gap layer # f_k(x, y), for VGG16, the shape is (1, 14, 14, 1024) conv_index = index_layer(model, cam_conv_layer_name) conv_output = model.layers[conv_index].output # Project the conv output to image space resized_output = K.resize_images(conv_output, ratio, ratio, 'tf') # The weights of GAP layer to softmax layer(ignore bias), the shape is (1024, num_classes) weights = model.layers[-1].weights[0] # Get the weighted conv activations classmap = K.dot(resized_output, weights) # Define the function get_cmap = K.function([K.learning_phase(), inc], [classmap]) # Get class map in image space im_cam = get_cmap([0, X])[0] # Only show the positive activations im_cam = np.clip(im_cam, 0, im_cam.max()) # Use the label that you want to visualize im_cam = im_cam[0, :, :, label] print('Info: Class map shape:', im_cam.shape) # Blur the map im_cam = ndimage.gaussian_filter(im_cam, sigma=(5), order=0) return im_cam
def max_depool(self, in_layer=None, depth_factor=2, height_factor=2, width_factor=2): if in_layer == None: in_layer = self.layer if len(self.layer.get_shape()) == 4: self.layer = kb.resize_images(self.layer, height_factor, width_factor, 'channels_last') print("Max Depool : {}".format(self.layer.get_shape())) if len(self.layer.get_shape()) == 5: self.layer = kb.resize_volumes(self.layer, depth_factor, height_factor, width_factor, 'channels_last') print("Max Depool : {}".format(self.layer.get_shape())) return self.layer
def pyramid_pooling_block(input_tensor, Bin_sizes): concat_list = [input_tensor] #w and h are size of input_tensor size = input_tensor.shape h = size[1] w = size[2] # mapping compatible bin_sizes factor = np.gcd(h, w) bin_sizes = [ x if ((h % x == 0) and (w % x == 0)) else 0 for x in Bin_sizes ] bin_sizes = list(filter((0).__ne__, bin_sizes)) if (1 not in bin_sizes): bin_sizes.insert(0, 1) ## TODO: need to add algo to add the next level compatible pyramid factor #bin_sizes.append(bin_sizes[-1]+factor) #while(len(bin_sizes)!=len(Bin_sizes)): for bin_size in bin_sizes: #TODO print("\n\n") print(size) print(w) print(h) print("\n\n") x = keras.layers.AveragePooling2D( pool_size=(h // bin_size, w // bin_size), strides=(h // bin_size, w // bin_size))(input_tensor) x = keras.layers.Conv2D( 128, 3, strides=1, padding='same', kernel_regularizer=keras.regularizers.l2(0.00004))(x) x = keras.layers.BatchNormalization()(x) x = ReLU()(x) x = keras.layers.Lambda( lambda x: K.resize_images(x, (h // x.shape[1]), (w // x.shape[2]), data_format='channels_last', interpolation='bilinear'))(x) concat_list.append(x) return keras.layers.concatenate(concat_list)
def pyramid_pooling_block(input_tensor, bin_sizes): concat_list = [input_tensor] w = 32 h = 32 for bin_size in bin_sizes: x = keras.layers.AveragePooling2D( pool_size=(w // bin_size, h // bin_size), strides=(w // bin_size, h // bin_size))(input_tensor) x = keras.layers.Conv2D(128, kernel_size=3, strides=2, padding='same')(x) x = keras.layers.Lambda( lambda x: K.resize_images(x, (h // x.shape[1]), (w // x.shape[2]), data_format='channels_last', interpolation='bilinear'))(x) concat_list.append(x) return keras.layers.concatenate(concat_list)
def _backward_pass(self, X, target_layer, d_switch, feat_map): # Run deconv/maxunpooling until input pixel space layer_index = self.lnames.index(target_layer) # Get the output of the target_layer of interest if layer_index == len(self.lnames)-1: print 'use none softmax activation' layer_output = K.function([self[self.lnames[0]].input,K.learning_phase()], K.dot(self[self.lnames[-2]].output,self[target_layer].W)+self[target_layer].b) else: layer_output = K.function([self[self.lnames[0]].input,K.learning_phase()], self[target_layer].output) X_outl = layer_output([X,1]) # Special case for the starting layer where we may want # to switchoff somes maps/ activations softmax_offset = np.log((1.0/(1.0-SOFT_MAX_EPS)-1.0)/(X_outl.shape[1]-1)) print "Deconvolving from %s..." % target_layer if feat_map is not None: print "Set other activation than channel %d to 0" % feat_map for i in range(X_outl.shape[1]): if i != feat_map: if len(X_outl.shape)<4: if layer_index == len(self.lnames)-1: print 'use softmax offset',softmax_offset X_outl[:,i] = -abs(softmax_offset)/2 #X_outl[:,i] = 0 else: X_outl[:,i,:,:] = 0 else: #pass if layer_index == len(self.lnames)-1: print 'use softmax offset',softmax_offset X_outl[:,i] = abs(softmax_offset)/2 #print X_outl[:,feat_map] #print X_outl[:,1-feat_map] # Iterate over layers (deepest to shallowest) batch_size = X_outl.shape[0] for lname in self.lnames[:layer_index+1][::-1]: print "Deconvolving %s..." % lname # Unpool, Deconv or do nothing; unflatten, see dense as conv if "flatten" in lname: ishape = self[lname].input_shape unflatten = K.function([self.flatX,K.learning_phase()], K.reshape(self.flatX, [batch_size]+[s for s in ishape[1:]])) X_outl = unflatten([X_outl,1]) elif "maxpooling2d" in lname: p1, p2 = self[lname].pool_size uppool = K.function([self.x,self.mask,K.learning_phase()], K.resize_images(self.x, p1, p2, "th")*self.mask) X_outl = uppool([X_outl,d_switch[lname],1]) #print d_switch[lname][0,feat_map,:,:] #print X_outl[0,feat_map,:,:] elif "convolution2d" in lname: X_outl = self._deconv(X_outl, lname) elif "padding" in lname: pass elif "dropout" in lname: pass elif "dense" in lname: X_outl = self._dedense(X_outl, lname, input_c = self.input_shape_withoutpadding[lname][1]) else: raise ValueError( "Invalid layer name: %s \n Can only handle maxpool and conv" % lname) print X_outl.shape return X_outl
def call(self, x, mask=None): # TODO: implement for dim_ordering='tf' img = K.resize_images(x, self.size[0], self.size[1], self.dim_ordering) padded = T.zeros((img.shape[0], img.shape[1], self.shape[0], self.shape[1])) padded = T.set_subtensor(padded[:, :, :img.shape[2], :img.shape[3]], img) return T.switch(self.ind, padded, T.zeros_like(padded))