def recall_loss(y_true, y_pred): ''' input: y_true (theano Tensor), y_pred (theano Tensor) output: recall_loss (float) ''' # print(K.ndim(y_true), K.ndim(y_pred)) return -np.log(K.mean(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1))))
def cat_acc(y, z): """Compute categorical accuracy given one-hot matrices.""" weights = _cat_sample_weights(y) _acc = K.cast(K.equal(K.argmax(y, axis=-1), K.argmax(z, axis=-1)), K.floatx()) _acc = K.sum(_acc * weights) / K.sum(weights) return _acc
def squeezed_accuracy(y_true, y_pred): class_id_true = K.argmax(K.squeeze(y_true,axis=0), axis=-1) class_id_preds = K.argmax(K.squeeze(y_pred,axis=0), axis=-1) # Replace class_id_preds with class_id_true for recall here # accuracy_mask = K.cast(K.equal(class_id_preds, interesting_class_id), 'int32') # class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') class_acc = K.mean(K.equal(class_id_true, class_id_preds)) return class_acc
def masked_categorical_accuracy(y_true, y_pred, mask): y_true = K.argmax(y_true, axis=-1) y_pred = K.argmax(y_pred, axis=-1) error = K.equal(y_true, y_pred) mask_template = T.and_(T.neq(y_true, mask), T.neq(y_true, 0)).nonzero() return K.mean(error[mask_template])
def fn(y_true, y_pred): class_id_true = K.argmax(y_true, axis=-1) class_id_preds = K.argmax(y_pred, axis=-1) # Replace class_id_preds with class_id_true for recall here accuracy_mask = K.cast(K.equal(class_id_true, interesting_class_id), 'int32') # accuracy_mask = K.cast(K.equal(class_id_true, interesting_class_id), 'int32') class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask class_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1) return class_acc
def dice(self, y_true, y_pred): """ compute dice for given Tensors """ if self.crop_indices is not None: y_true = utils.batch_gather(y_true, self.crop_indices) y_pred = utils.batch_gather(y_pred, self.crop_indices) if self.input_type == 'prob': # We assume that y_true is probabilistic, but just in case: y_true /= K.sum(y_true, axis=-1, keepdims=True) y_true = K.clip(y_true, K.epsilon(), 1) # make sure pred is a probability y_pred /= K.sum(y_pred, axis=-1, keepdims=True) y_pred = K.clip(y_pred, K.epsilon(), 1) # Prepare the volumes to operate on # If we're doing 'hard' Dice, then we will prepare one-hot-based matrices of size # [batch_size, nb_voxels, nb_labels], where for each voxel in each batch entry, # the entries are either 0 or 1 if self.dice_type == 'hard': # if given predicted probability, transform to "hard max"" if self.input_type == 'prob': if self.approx_hard_max: y_pred_op = _hard_max(y_pred, axis=-1) y_true_op = _hard_max(y_true, axis=-1) else: y_pred_op = _label_to_one_hot(K.argmax(y_pred, axis=-1), self.nb_labels) y_true_op = _label_to_one_hot(K.argmax(y_true, axis=-1), self.nb_labels) # if given predicted label, transform to one hot notation else: assert self.input_type == 'max_label' y_pred_op = _label_to_one_hot(y_pred, self.nb_labels) y_true_op = _label_to_one_hot(y_true, self.nb_labels) # If we're doing soft Dice, require prob output, and the data already is as we need it # [batch_size, nb_voxels, nb_labels] else: assert self.input_type == 'prob', "cannot do soft dice with max_label input" y_pred_op = y_pred y_true_op = y_true # compute dice for each entry in batch. # dice will now be [batch_size, nb_labels] sum_dim = 1 top = 2 * K.sum(y_true_op * y_pred_op, sum_dim) bottom = K.sum(K.square(y_true_op), sum_dim) + K.sum(K.square(y_pred_op), sum_dim) # make sure we have no 0s on the bottom. K.epsilon() bottom = K.maximum(bottom, self.area_reg) return top / bottom
def sparse_accuracy_ignoring_last_label(y_true, y_pred): nb_classes = K.int_shape(y_pred)[-1] y_pred = K.reshape(y_pred, (-1, nb_classes)) y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), nb_classes + 1) unpacked = tf.unstack(y_true, axis=-1) legal_labels = ~tf.cast(unpacked[-1], tf.bool) y_true = tf.stack(unpacked[:-1], axis=-1) return K.sum(tf.to_float(legal_labels & K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)))) / K.sum(tf.to_float(legal_labels))
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6): """Filters YOLO boxes by thresholding on object and class confidence. Arguments: box_confidence -- tensor of shape (19, 19, 5, 1) boxes -- tensor of shape (19, 19, 5, 4) box_class_probs -- tensor of shape (19, 19, 5, 80) threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box Returns: scores -- tensor of shape (None,), containing the class probability score for selected boxes boxes -- tensor of shape (None, 4), containing (b_x, b_y, b_h, b_w) coordinates of selected boxes classes -- tensor of shape (None,), containing the index of the class detected by the selected boxes Note: "None" is here because you don't know the exact number of selected boxes, as it depends on the threshold. For example, the actual output size of scores would be (10,) if there are 10 boxes. """ # Step 1: Compute box scores box_scores = box_confidence * box_class_probs # [19, 19, 5, 1] * [19, 19, 5, 80] = [19, 19, 5, 80] # Step 2: Find the box_classes thanks to the max box_scores, keep track of the corresponding score box_classes = K.argmax(box_scores, axis=-1) box_class_scores = K.max(box_scores, axis = -1, keepdims = False) # Step 3: Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the # same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold) filtering_mask = box_class_scores >= threshold # Step 4: Apply the mask to scores, boxes and classes scores = tf.boolean_mask(box_class_scores, filtering_mask) boxes = tf.boolean_mask(boxes, filtering_mask) classes = tf.boolean_mask(box_classes, filtering_mask) return scores, boxes, classes
def call(self, x): assert isinstance(x, list) inp_a, inp_b = x outp_a = K.l2_normalize(inp_a, -1) outp_b = K.l2_normalize(inp_b, -1) alpha = K.batch_dot(outp_b, outp_a, axes=[2, 2]) alpha = K.l2_normalize(alpha, 1) alpha = K.one_hot(K.argmax(alpha, 1), K.int_shape(inp_a)[1]) hmax = K.batch_dot(alpha, outp_b, axes=[1, 1]) kcon = K.eye(K.int_shape(inp_a)[1], dtype='float32') m = [] for i in range(self.output_dim): outp_a = inp_a * self.W[i] outp_hmax = hmax * self.W[i] outp_a = K.l2_normalize(outp_a, -1) outp_hmax = K.l2_normalize(outp_hmax, -1) outp = K.batch_dot(outp_hmax, outp_a, axes=[2, 2]) outp = K.sum(outp * kcon, -1, keepdims=True) m.append(outp) if self.output_dim > 1: persp = K.concatenate(m, 2) else: persp = m return [persp, persp]
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6): """Filters YOLO boxes by thresholding on object and class confidence. Arguments: box_confidence -- tensor of shape (19, 19, 5, 1) boxes -- tensor of shape (19, 19, 5, 4) box_class_probs -- tensor of shape (19, 19, 5, 80) threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box Returns: scores -- tensor of shape (None,), containing the class probability score for selected boxes boxes -- tensor of shape (None, 4), containing (b_x, b_y, b_h, b_w) coordinates of selected boxes classes -- tensor of shape (None,), containing the index of the class detected by the selected boxes """ # Step 1: Compute box scores box_scores = box_confidence*box_class_probs # Step 2: Find the box_classes thanks to the max box_scores, keep track of the corresponding score box_classes = K.argmax(box_scores, axis=-1) box_class_scores = K.max(box_scores, axis=-1) # Step 3: Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the # same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold) filtering_mask = box_class_scores >= threshold # Step 4: Apply the mask to scores, boxes and classes scores = tf.boolean_mask(box_class_scores,filtering_mask) boxes = tf.boolean_mask(boxes,filtering_mask) classes = tf.boolean_mask(box_classes,filtering_mask) return scores, boxes, classes
def rec_L(y_true, y_pred): s_flow = K.variable(np.array([1,0])) p = K.cast(K.equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx()) n = K.cast(K.not_equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx()) t = K.cast(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx()) f = K.cast(K.not_equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx()) tn = t*n fp = f*p return K.sum(tn) / (K.sum(tn) + K.sum(fp))
def rec_S(y_true, y_pred): s_flow = K.variable(np.array([1,0])) p = K.cast(K.equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx()) n = K.cast(K.not_equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx()) t = K.cast(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx()) f = K.cast(K.not_equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx()) tp = t*p fn = f*n return K.sum(tp) / (K.sum(tp) + K.sum(fn))
def _test_no_grad(optimizer): inp = Input([3]) x = Dense(10)(inp) x = Lambda(lambda l: 1.0 * K.reshape(K.cast(K.argmax(l), 'float32'), [-1, 1]))(x) mod = Model(inp, x) mod.compile(optimizer, 'mse') with pytest.raises(ValueError): mod.fit(np.zeros([10, 3]), np.zeros([10, 1], np.float32), batch_size=10, epochs=10)
def _find_maxima(x): x = K.cast(x, K.floatx()) col_max = K.max(x, axis=1) row_max = K.max(x, axis=2) maxima = K.max(col_max, 1) maxima = K.expand_dims(maxima, -2) cols = K.cast(K.argmax(col_max, -2), K.floatx()) rows = K.cast(K.argmax(row_max, -2), K.floatx()) cols = K.expand_dims(cols, -2) rows = K.expand_dims(rows, -2) maxima = K.concatenate([rows, cols, maxima], -2) return maxima
def yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold=.6): """Filter YOLO boxes based on object and class confidence.""" box_scores = box_confidence * box_class_probs box_classes = K.argmax(box_scores, axis=-1) box_class_scores = K.max(box_scores, axis=-1) prediction_mask = box_class_scores >= threshold # TODO: Expose tf.boolean_mask to Keras backend? boxes = tf.boolean_mask(boxes, prediction_mask) scores = tf.boolean_mask(box_class_scores, prediction_mask) classes = tf.boolean_mask(box_classes, prediction_mask) return boxes, scores, classes
def find_patch_matches(a, a_norm, b): '''For each patch in A, find the best matching patch in B''' convs = None if K.backend() == 'theano': # HACK: This was not being performed on the GPU for some reason. from theano.sandbox.cuda import dnn if dnn.dnn_available(): convs = dnn.dnn_conv( img=a, kerns=b[:, :, ::-1, ::-1], border_mode='valid') if convs is None: convs = K.conv2d(a, b[:, :, ::-1, ::-1], border_mode='valid') argmax = K.argmax(convs / a_norm, axis=1) return argmax
def compute_error_matrix(y_true, y_pred): """Compute Confusion matrix (a.k.a. error matrix). a predicted c 0 1 2 t 0 [[ 5, 3, 0], u 1 [ 2, 3, 1], a 2 [ 0, 2, 11]] l Note true positves are in diagonal """ # Find channel axis given backend if K.image_data_format() == 'channels_last': ax_chn = 3 else: ax_chn = 1 classes = y_true.shape[ax_chn] confusion = get_confusion(K.argmax(y_true, axis=ax_chn).flatten(), K.argmax(y_pred, axis=ax_chn).flatten(), classes) return confusion
def call(self, inputs, **kwargs): if type(inputs) is list: # true label is provided with shape = [None, n_classes], i.e. one-hot code. assert len(inputs) == 2 inputs, mask = inputs else: # if no true label, mask by the max length of capsules. Mainly used for prediction # compute lengths of capsules x = K.sqrt(K.sum(K.square(inputs), -1)) # generate the mask which is a one-hot code. # mask.shape=[None, n_classes]=[None, num_capsule] mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1]) # inputs.shape=[None, num_capsule, dim_capsule] # mask.shape=[None, num_capsule] # masked.shape=[None, num_capsule * dim_capsule] masked = K.batch_flatten(inputs * K.expand_dims(mask, -1)) return masked
def build(self): question = self.question answer = self.get_answer() rnn_model = get_model(question_maxlen=self.model_params.get('question_len', 20), answer_maxlen=self.model_params.get('question_len', 60), vocab_len=self.config['n_words'], n_hidden=256, load_save=True) rnn_model.trainable = False answer_inverted = rnn_model(answer) argmax = Lambda(lambda x: K.argmax(x, axis=2), output_shape=lambda x: (x[0], x[1])) argmax.trainable = False answer_argmax = argmax(answer_inverted) # add embedding layers weights = self.model_params.get('initial_embed_weights', None) weights = weights if weights is None else [weights] embedding = Embedding(input_dim=self.config['n_words'], output_dim=self.model_params.get('n_embed_dims', 100), # W_regularizer=regularizers.activity_l1(1e-4), W_constraint=constraints.nonneg(), dropout=0.5, weights=weights, mask_zero=True) question_embedding = embedding(question) answer_embedding = embedding(answer_argmax) # maxpooling maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2])) question_maxpool = maxpool(question_embedding) answer_maxpool = maxpool(answer_embedding) # activation activation = Activation('linear') question_output = activation(question_maxpool) answer_output = activation(answer_maxpool) return question_output, answer_output
loss='categorical_crossentropy', metrics=['accuracy']) train_datagen = ImageDataGenerator() train_generator = train_datagen.flow_from_directory( directory=TRAIN_PATH, target_size=(IMAGE_SIZE, IMAGE_SIZE), classes=['bus', 'dinosaur', 'flower', 'horse', 'elephant']) test_datagen = ImageDataGenerator() test_generator = test_datagen.flow_from_directory( directory=TEST_PATH, target_size=(IMAGE_SIZE, IMAGE_SIZE), classes=['bus', 'dinosaur', 'flower', 'horse', 'elephant']) model.fit_generator(train_generator, epochs=5, validation_data=test_generator) img = load_img(path=PREDICT_IMG, target_size=(IMAGE_SIZE, IMAGE_SIZE)) x = img_to_array(img) x = K.expand_dims(x, axis=0) x = preprocess_input(x) result = model.predict(x, steps=1) class_x = K.eval(K.argmax(result)) classes = ['bus', 'dinosaur', 'flower', 'horse', 'elephant'] a = np.array(classes)[class_x] test_image = mpimg.imread(PREDICT_IMG) plt.imshow(test_image) plt.show() print("result:", a)
def compute_acc(y_true,y_pred): return K.mean(K.in_top_k(y_pred, K.argmax(y_true, axis=-1), k), axis=-1)
def acc_pred(y_true, y_pred): target = y_true[..., 1:] output = y_pred[..., 1:] return K.cast(K.equal(K.argmax(target, -1), K.argmax(output, -1)), K.floatx())
def one_hot(x, n_val = 78): x = K.argmax(x) x = tf.one_hot(x, n_val) x = RepeatVector(1)(x) return x
def FP(y_true, y_pred): y_true = K.argmax(y_true, axis=-1) y_pred = K.argmax(y_pred, axis=-1) return K.sum(K.cast(K.less(y_true, y_pred), K.floatx()))
def argmax(self): return Lambda(lambda x: K.expand_dims(K.argmax(x)), output_shape=(None, 1), name="u%s" % self.nr)(self.p)
def argmax(x): ''' Helper function to build the `AbsVal` activation layer ''' return K.argmax(x, axis=-1)
def call(self, x): print('x', x, type(x)) preds = K.argmax(x, axis=self._axis) return preds
def __init__(self, context_size, melody_bar_len, meta_embed_size, bar_embedder, rhythm_net, melody_net, meta_predictor, generation=False, compile_now=True): if generation and compile_now: raise ValueError("Cannot be used to generate and train at the same time!") if not rhythm_net.use_meta and not melody_net.use_meta: raise NotImplementedError("Neither RhythmNet nor MelodyNet are using metaData."+ " This is not implemented!") if not rhythm_net.n_voices == melody_net.n_voices == meta_embed_size: raise ValueError("n_voices not the same for all networks:" + " ,".join((str(rhythm_net.n_voices), str(melody_net.n_voices), str(meta_embed_size)))) rhythm_contexts = [Input(shape=(None,), name="rythm_context_"+str(i)) for i in range(context_size)] melody_contexts = Input(shape=(None, melody_bar_len), name="melody_contexts") meta_embedded = Input(shape=(meta_embed_size,), name="meta_embedded") lead_rhythm = Input(shape=(None, ), name="lead_rhythm") lead_melody = Input(shape=(None, melody_bar_len), name="lead_melody") rhythm_preds = rhythm_net([*rhythm_contexts, meta_embedded, lead_rhythm]) if generation: rhythms_from_net = Lambda(lambda probs: K.argmax(probs), output_shape=(None,))(rhythm_preds) rhythms_embedded = bar_embedder(rhythms_from_net) else: rhythms = Input(shape=(None,), name="rhythms") rhythms_embedded = bar_embedder(rhythms) melody_preds = melody_net([melody_contexts, rhythms_embedded, meta_embedded, lead_melody]) if generation: super().__init__(inputs=[*rhythm_contexts, melody_contexts, meta_embedded, lead_rhythm, lead_melody], outputs=[rhythm_preds, melody_preds]) else: meta_embedded_prev = Input(shape=(meta_embed_size, )) meta_embedded_recon = meta_predictor([rhythm_preds, melody_preds, meta_embedded_prev]) super().__init__(inputs=[*rhythm_contexts, rhythms, melody_contexts, meta_embedded, meta_embedded_prev, lead_rhythm, lead_melody], outputs=[rhythm_preds, melody_preds, meta_embedded_recon]) self.bar_embedder = bar_embedder self.rhythm_net = rhythm_net self.melody_net = melody_net self.meta_predictor = meta_predictor self.params = {"context_size": context_size, "melody_bar_len": melody_bar_len, "meta_embed_size": meta_embed_size, "bar_embed_params": self.bar_embedder.params, "rhythm_net_params": self.rhythm_net.params, "melody_net_params": self.melody_net.params} if compile_now: self.compile_default()
prop = K.variable([[21, 39, 38, 50], [1, 2, 10, 10], [19, 40, 42, 49], [9, 8, 19, 40], [0, 2, 8, 10], [10, 10, 18, 42]], dtype='float32') iou_mat = K.transpose(iou_matrix(prop, bbox_gt)) max_iou = K.max(iou_mat, axis=1) positive_idxs = tf.where(max_iou >= 0.5)[:, 0] negative_idxs = tf.where(max_iou < 0.5)[:, 0] argmax = K.argmax(K.gather(iou_mat, positive_idxs), axis=1) positive_bboxes_gt = K.gather(bbox_gt, argmax) ts = target_shift(prop, positive_bboxes_gt) print("Proposals") print(prop.eval(session=K.get_session())) print("\nBboxes") print(positive_bboxes_gt.eval(session=K.get_session())) print("\nShifts") print(ts.eval(session=K.get_session())) print("\nTransformed Shifts")
def find_patch_matches(a, a_norm, b): '''For each patch in A, find the best matching patch in B''' # we want cross-correlation here so flip the kernels convs = K.conv2d(a, b[:, :, ::-1, ::-1], border_mode='valid') argmax = K.argmax(convs / a_norm, axis=1) return argmax
import tensorflow as tf from keras import backend as K with tf.Session() as test_a: box_confidence = tf.constant([[[[10, 20]], [[11, 21]], [[12, 22]]], [[[40, 50]], [[410, 51]], [[42, 52]]], [[[70, 80]], [[71, 81]], [[72, 82]]]]) print("box_confidence.shape = " + str(box_confidence.shape)) #print("box_confidence =\n" + str(test_a.run(box_confidence))) box_classes = K.argmax(box_confidence, axis=-1) box_class_scores = K.max(box_confidence, axis=-1) print("box_classes.shape: " + str(box_classes.shape)) print("box_classes[0, :] = " + str(test_a.run(box_classes))) print("box_classes[0, :] = " + str(test_a.run(box_classes[0, :]))) print("box_classes[0, 0, :] = " + str(test_a.run(box_classes[0, 0, :]))) print("box_classes[0, 0, 0] = " + str(test_a.run(box_classes[0, 0, 0]))) print("box_class_scores.shape: " + str(box_class_scores.shape)) print("box_class_scores[0, :] = " + str(test_a.run(box_class_scores))) print("box_class_scores[0, :] = " + str(test_a.run(box_class_scores[0, :]))) print("box_class_scores[0, 0, :] = " + str(test_a.run(box_class_scores[0, 0, :]))) print("box_class_scores[0, 0, 0] = " + str(test_a.run(box_class_scores[0, 0, 0]))) filtering_mask = (tf.cast(box_class_scores, tf.float32) > 400) print("filtering_mask.shape: " + str(filtering_mask.shape)) print("filtering_mask[0, :] = " + str(test_a.run(filtering_mask)))
def FN(y_true, y_pred): y_true = K.argmax(y_true, axis=-1) y_pred = K.argmax(y_pred, axis=-1) return K.sum(K.cast(K.greater(y_true, y_pred), K.floatx()))
def top_k_categorical_accuracy(y_true, y_pred, k=5): return K.mean(K.in_top_k(y_pred, K.argmax(y_true, axis=-1), k))
def simple_test(image_path): image = cv2.imread(image_path, cv2.IMREAD_COLOR) height = image.shape[1] width = image.shape[0] image = cv2.resize(image, (image_w,image_h)) image = image.reshape((1,image_w,image_h,3)) prediction = model.predict(image, batch_size=1) print(prediction.shape) # 1, 13, 13, 125 # Reshape it to 1,13,13,5,25 # 5 anchor boxes at every grid in 13 x 13 # 25 elements of reach anchorbox # probabiliity if an object is present, bx, by, w, h, 20 dim vector for each class p_resh = prediction.reshape(1, 13, 13, 5, 25) print(p_resh.shape) for box_i in range(5): box = p_resh[0][0][0][box_i] pc = box[0] c_scores = box[5:] res = pc * c_scores idx = np.argmax(res) p = class_dict[idx] print("Box No {} score {} box {},{},{},{} class {} ".format(box_i, res[idx], box[1],box[2],box[3],box[4], p)) box_confidence = p_resh[:,:,:,:,0] box_confidence = box_confidence.reshape(1,13,13,5,1) boxes = p_resh[:,:,:,:,1:5] boxes = boxes.reshape(1,13,13,5,4) box_class_prob = p_resh[:,:,:,:,5:] box_class_prob = box_class_prob.reshape(1,13,13,5,20) # Filter the boxes threshold = 0.6 box_scores = np.multiply(box_confidence, box_class_prob) print(box_scores.shape) box_class = K.argmax(box_scores, axis =-1) box_class_scores = K.max(box_scores, axis=-1) # Filtering mask filtering_mask = K.greater_equal(box_class_scores, threshold) with K.get_session() as test: scores = tf.boolean_mask(box_class_scores, filtering_mask).eval() boxes = tf.boolean_mask(boxes, filtering_mask).eval() classes = tf.boolean_mask(box_class, filtering_mask).eval() print(boxes.shape) print(classes.shape) print(scores.shape) max_boxes = 5 iou_threshold = 0.6 max_boxes_tensor = K.variable(max_boxes, dtype='int32') # tensor to be used in tf.image.non_max_suppression() test.run(tf.variables_initializer([max_boxes_tensor]))# initialize variable max_boxes_tensor # Use tf.image.non_max_suppression() to get the list of indices corresponding to boxes you keep nms_indices = tf.image.non_max_suppression(boxes, scores, max_boxes_tensor, iou_threshold=iou_threshold) scores = K.gather(scores, nms_indices).eval() boxes = K.gather(boxes, nms_indices).eval() classes = K.gather(classes, nms_indices).eval() print(boxes.shape) print(classes.shape) print(scores.shape) # scale the boxes image_dims = K.stack([height, width, height, width]) image_dims = K.reshape(image_dims, [1, 4]) boxes = boxes * image_dims print(boxes.eval())
def categorical_accuracy(y_true, y_pred): return K.mean(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)))
def building_accuracy(y_true, y_pred): idx_true = K.argmax(y_true, axis=-1) idx_pred = K.argmax(y_pred, axis=-1) bld_true = tf.map_fn(bld_idx, idx_true) bld_pred = tf.map_fn(bld_idx, idx_pred) return K.cast(K.equal(bld_true, bld_pred), K.floatx())
def argmax_fun(softmax_output): return K.argmax(softmax_output, axis=2)
def TN(y_true, y_pred): y_true = K.argmax(y_true, axis=-1) y_pred = K.argmax(y_pred, axis=-1) return K.sum(K.cast(K.equal(y_true + y_pred, 0), K.floatx()))
def first_q_acc(y_true, y_pred): return K.mean(K.equal(K.argmax(y_true, axis=-1)[:,0], K.argmax(y_pred, axis=-1)[:,0]))
def f1_0(y_true, y_pred): id_true = K.argmax(y_true, axis=-1) id_pred = K.argmax(y_pred, axis=-1) return single_f1(0, id_true, id_pred)
def floor_accuracy(y_true, y_pred): idx_true = K.argmax(y_true, axis=-1) idx_pred = K.argmax(y_pred, axis=-1) flr_true = tf.map_fn(flr_idx, idx_true) flr_pred = tf.map_fn(flr_idx, idx_pred) return K.cast(K.equal(flr_true, flr_pred), K.floatx())
from keras.models import load_model from keras.applications.vgg16 import preprocess_input from keras.preprocessing.image import load_img, img_to_array import keras.backend as K model_path = 'My_VGG16_Model.h5' model = load_model(model_path) model.summary() PREDICT_IMG = 'data/flower.jpg' # 找一张图片进行预测验证 img = load_img(path=PREDICT_IMG, target_size=(224, 224)) # 转换成numpy数组 x = img_to_array(img) # 转换后的数组为3维数组(224,224,3), # 而训练的数组为4维(图片数量, 224,224, 3),所以我们可扩充下维度 x = K.expand_dims(x, axis=0) # 需要被预处理下 x = preprocess_input(x) # 数据预测 result = model.predict(x, steps=1) print(result[0]) # 最后的结果是一个含有5个数的一维数组,我们取最大值所在的索引号,即对应'bus', 'dinosaur', 'flower', 'horse', 'elephant'的顺序 print("result:", K.eval(K.argmax(result)))
], [ [410, 411, 412, 413], [510, 511, 512, 513] ], [ [420, 421, 422, 423], [520, 521, 522, 523] ] ], [ [ [700, 701, 702, 703], [800, 801, 802, 803] ], [ [710, 711, 712, 713], [810, 811, 812, 813] ], [ [720, 721, 722, 723], [820, 821, 822, 823] ] ] ] ) print("box_class_probs.shape = " + str(box_class_probs.shape)) #print("box_class_probs =\n" + str(test_a.run(boxes))) box_scores = box_confidence * box_class_probs box_classes = K.argmax(box_scores, axis=-1) print("box_classes.shape: " + str(box_classes.shape)) print("box_classes[0, :] = " + str(test_a.run(box_classes[0, :]))) print("box_classes[0, 0, :] = " + str(test_a.run(box_classes[0, 0, :]))) print("box_classes[0, 0, 0] = " + str(test_a.run(box_classes[0, 0, 0]))) print("box_scores.shape: " + str(box_scores.shape)) print("box_scores: " + str(test_a.run(box_scores))) #print("box_classes = " + str(test_a.run(box_classes)))
def categorical_accuracy_per_sequence(y_true, y_pred): return K.mean(K.min(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), axis=-1))
def compile(self, optimizer, loss, class_mode="categorical", sample_weight_mode=None): '''Configure the learning process. # Arguments optimizer: str (name of optimizer) or optimizer object. See [optimizers](optimizers.md). loss: str (name of objective function) or objective function. See [objectives](objectives.md). class_mode: one of "categorical", "binary". This is only used for computing classification accuracy or using the predict_classes method. sample_weight_mode: if you need to do timestep-wise sample weighting (2D weights), set this to "temporal". "None" defaults to sample-wise weights (1D). ''' self.optimizer = optimizers.get(optimizer) self.sample_weight_mode = sample_weight_mode self.loss = objectives.get(loss) weighted_loss = weighted_objective(self.loss) # input of model self.X_train = self.get_input(train=True) self.X_test = self.get_input(train=False) self.single_y_train = self.get_output(train=True) self.single_y_test = self.get_output(train=False) self.diff_train = K.placeholder(ndim=1) self.diff_test = K.placeholder(ndim=1) self.y_train = K.concatenate([ K.dot(self.diff_train, self.single_y_train[:self.diff_train.shape[0]]), K.dot(self.diff_train, self.single_y_train[self.diff_train.shape[0]:]) ], axis=0) self.y_test = K.concatenate([ K.dot(self.diff_test, self.single_y_test[:self.diff_test.shape[0]]), K.dot(self.diff_test, self.single_y_test[self.diff_test.shape[0]:]) ], axis=0) # target of model self.y = K.placeholder(ndim=K.ndim(self.y_train)) if self.sample_weight_mode == 'temporal': self.weights = K.placeholder(ndim=2) else: self.weights = K.placeholder(ndim=1) if hasattr(self.layers[-1], "get_output_mask"): mask = self.layers[-1].get_output_mask() else: mask = None train_loss = weighted_loss(self.y, self.y_train, self.weights, mask) test_loss = weighted_loss(self.y, self.y_test, self.weights, mask) if class_mode == "categorical": train_accuracy = K.mean( K.equal(K.argmax(self.y, axis=-1), K.argmax(self.y_train, axis=-1))) test_accuracy = K.mean( K.equal(K.argmax(self.y, axis=-1), K.argmax(self.y_test, axis=-1))) elif class_mode == "binary": train_accuracy = K.mean(K.equal(self.y, K.round(self.y_train))) test_accuracy = K.mean(K.equal(self.y, K.round(self.y_test))) else: raise Exception("Invalid class mode:" + str(class_mode)) self.class_mode = class_mode for r in self.regularizers: train_loss = r(train_loss) updates = self.optimizer.get_updates(self.trainable_weights, self.constraints, train_loss) updates += self.updates if type(self.X_train) == list: train_ins = self.X_train + [self.diff_train, self.y, self.weights] test_ins = self.X_test + [self.diff_test, self.y, self.weights] assert type(self.X_test) == list predict_ins = self.X_test + [self.diff_test] else: train_ins = [self.X_train, self.diff_train, self.y, self.weights] test_ins = [self.X_test, self.diff_test, self.y, self.weights] predict_ins = [self.X_test, self.diff_test] self.__train = K.function(train_ins, [train_loss], updates=updates) self.__train_with_acc = K.function(train_ins, [train_loss, train_accuracy], updates=updates) self.__predict = K.function(predict_ins, [self.y_test], updates=self.state_updates) self.__test = K.function(test_ins, [test_loss], updates=self.state_updates) self.__test_with_acc = K.function(test_ins, [test_loss, test_accuracy], updates=self.state_updates) self._train = lambda rr: self.__train([r[0] for r in rr[:-1]] + [rr[-1]]) self._train_with_acc = lambda rr: self.__train_with_acc( [r[0] for r in rr[:-1]] + [rr[-1]]) self._predict = lambda rr: self.__predict([r[0] for r in rr]) self._test = lambda rr: self.__test([r[0] for r in rr[:-1]] + [rr[-1]]) self._test_with_acc = lambda rr: self.__test_with_acc( [r[0] for r in rr[:-1]] + [rr[-1]])
def one_hot(x): x = K.argmax(x) x = tf.one_hot(x, 78) x = RepeatVector(1)(x) return x
def one_hot(x): x = K.argmax(x) x = tf.one_hot(x, n_x) return x
def true_acc(y_true, y_pred): y_true = K.flatten(K.argmax(y_true[:, 1:-1], axis=-1)) y_pred = K.flatten(K.argmax(y_pred[:, 1:-1], axis=-1)) mask = K.not_equal(y_true, K.zeros_like(y_true)) correct = K.cast(K.equal(tf.boolean_mask(y_true, mask), tf.boolean_mask(y_pred, mask)), 'float32') return K.sum(correct[:]) / K.cast(K.shape(correct)[0], 'float32')
outputs=[m.predictions, m.g, m.a, m.gb_grad]) model.summary() model.load_weights('my_model.h5') (x_train, y_train), (x_test, y_test) = mnist.load_data() x_test = x_test / 255. x_test = x_test.reshape((-1, 28, 28, 1)) y_test = keras.utils.to_categorical(y_test, num_classes) print(y_test.shape) for target_y_train_num in range(20): result = model.predict([ y_test[target_y_train_num].reshape((1, 10)), x_test[target_y_train_num].reshape((1, 28, 28, 1)) ]) print('answer: ', K.eval(K.argmax(y_test[target_y_train_num]))) print('prediction: ', K.eval(K.argmax(result[0]))) conv_grad = result[1] conv_grad = conv_grad.reshape(conv_grad.shape[1:]) conv_output = result[2] conv_output = conv_output.reshape(conv_output.shape[1:]) input_grad = result[3] input_grad = input_grad.reshape(input_grad.shape[1:]) gradRGB = gb_viz = input_grad from skimage.transform import resize import cv2 # global average pooling weights = np.mean(conv_grad, axis=(0, 1))
def my_accuracy(y_true, y_pred): # print(y_pred) return K.min( K.cast(K.equal(K.argmax(y_true, axis=2), K.argmax(y_pred, axis=2)), dtype="float32"))
loss = objectives.get("categorical_crossentropy") weighted_loss = models.weighted_objective(loss) y = K.placeholder(ndim=K.ndim(y_train)) weights = K.placeholder(ndim=1) train_loss = weighted_loss(y, y_train, weights, mask_train) test_loss = weighted_loss(y, y_test, weights, mask_test) _y_train = K.placeholder(ndim=3, name="y_train") _y_test = K.placeholder(ndim=3, name="y_test") _train_loss = weighted_loss(y, _y_train, weights, mask_train) _test_loss = weighted_loss(y, _y_test, weights, mask_test) print("train_loss:", P.pprint(_train_loss)) print("test_loss", P.pprint(_test_loss)) """categorical accuracy""" train_accuracy = K.mean(K.equal(K.argmax(y, axis=-1), K.argmax(y_train, axis=-1))) test_accuracy = K.mean(K.equal(K.argmax(y, axis=-1), K.argmax(y_test, axis=-1))) _train_accuracy = K.mean(K.equal(K.argmax(y, axis=-1), K.argmax(_y_train, axis=-1))) _test_accuracy = K.mean(K.equal(K.argmax(y, axis=-1), K.argmax(_y_test, axis=-1))) print("train_accuracy:", P.pprint(_train_accuracy)) print("test_accuracy", P.pprint(_test_accuracy)) """parameters""" params = [] regularizers = [] constraints = [] updates = [] state_updates = [] for layer in layers: _params, _regularizers, _consts, _updates = layer.get_params()
def chain_crf_loss(y, x, U, b_start=None, b_end=None, mask=None): """Variant of sparse_chain_crf_loss but with one-hot encoded tags y.""" y_sparse = K.argmax(y, -1) y_sparse = K.cast(y_sparse, 'int32') return sparse_chain_crf_loss(y_sparse, x, U, b_start, b_end, mask)
def sparse_categorical_accuracy(y_true, y_pred): return K.mean(K.equal(K.max(y_true, axis=-1), K.cast(K.argmax(y_pred, axis=-1), K.floatx())))
def sparse_accuracy(y_true, y_pred): import keras.backend as K return K.cast(K.equal(y_true[:, :, 0], K.cast(K.argmax(y_pred, axis=-1), y_true.dtype)), K.floatx())
def compile(self, optimizer, loss, class_mode="categorical", sample_weight_mode=None): '''Configure the learning process. # Arguments optimizer: str (name of optimizer) or optimizer object. See [optimizers](optimizers.md). loss: str (name of objective function) or objective function. See [objectives](objectives.md). class_mode: one of "categorical", "binary". This is only used for computing classification accuracy or using the predict_classes method. sample_weight_mode: if you need to do timestep-wise sample weighting (2D weights), set this to "temporal". "None" defaults to sample-wise weights (1D). ''' self.optimizer = optimizers.get(optimizer) self.sample_weight_mode = sample_weight_mode self.loss = objectives.get(loss) weighted_loss = weighted_objective(self.loss) # input of model self.X_train = self.get_input(train=True) self.X_test = self.get_input(train=False) self.single_y_train = self.get_output(train=True) self.single_y_test = self.get_output(train=False) self.diff_train = K.placeholder(ndim=1) self.diff_test = K.placeholder(ndim=1) self.y_train = K.concatenate( [K.dot(self.diff_train, self.single_y_train[:self.diff_train.shape[0]]), K.dot(self.diff_train, self.single_y_train[self.diff_train.shape[0]:])], axis=0) self.y_test = K.concatenate( [K.dot(self.diff_test, self.single_y_test[:self.diff_test.shape[0]]), K.dot(self.diff_test, self.single_y_test[self.diff_test.shape[0]:])], axis=0) # target of model self.y = K.placeholder(ndim=K.ndim(self.y_train)) if self.sample_weight_mode == 'temporal': self.weights = K.placeholder(ndim=2) else: self.weights = K.placeholder(ndim=1) if hasattr(self.layers[-1], "get_output_mask"): mask = self.layers[-1].get_output_mask() else: mask = None train_loss = weighted_loss(self.y, self.y_train, self.weights, mask) test_loss = weighted_loss(self.y, self.y_test, self.weights, mask) if class_mode == "categorical": train_accuracy = K.mean(K.equal(K.argmax(self.y, axis=-1), K.argmax(self.y_train, axis=-1))) test_accuracy = K.mean(K.equal(K.argmax(self.y, axis=-1), K.argmax(self.y_test, axis=-1))) elif class_mode == "binary": train_accuracy = K.mean(K.equal(self.y, K.round(self.y_train))) test_accuracy = K.mean(K.equal(self.y, K.round(self.y_test))) else: raise Exception("Invalid class mode:" + str(class_mode)) self.class_mode = class_mode for r in self.regularizers: train_loss = r(train_loss) updates = self.optimizer.get_updates(self.trainable_weights, self.constraints, train_loss) updates += self.updates if type(self.X_train) == list: train_ins = self.X_train + [self.diff_train, self.y, self.weights] test_ins = self.X_test + [self.diff_test, self.y, self.weights] assert type(self.X_test) == list predict_ins = self.X_test + [self.diff_test] else: train_ins = [self.X_train, self.diff_train, self.y, self.weights] test_ins = [self.X_test, self.diff_test, self.y, self.weights] predict_ins = [self.X_test, self.diff_test] self.__train = K.function(train_ins, [train_loss], updates=updates) self.__train_with_acc = K.function(train_ins, [train_loss, train_accuracy], updates=updates) self.__predict = K.function(predict_ins, [self.y_test], updates=self.state_updates) self.__test = K.function(test_ins, [test_loss], updates=self.state_updates) self.__test_with_acc = K.function(test_ins, [test_loss, test_accuracy], updates=self.state_updates) self._train = lambda rr: self.__train([r[0] for r in rr[:-1]] + [rr[-1]]) self._train_with_acc = lambda rr: self.__train_with_acc([r[0] for r in rr[:-1]] + [rr[-1]]) self._predict = lambda rr: self.__predict([r[0] for r in rr]) self._test = lambda rr: self.__test([r[0] for r in rr[:-1]] + [rr[-1]]) self._test_with_acc = lambda rr: self.__test_with_acc([r[0] for r in rr[:-1]] + [rr[-1]])
def get_acc(args): y_pred, y_true = args mask = tf.cast(tf.not_equal(y_true, 0), 'float32') corr = K.cast(K.equal(K.cast(y_true, 'int32'), K.cast(K.argmax(y_pred, axis=-1), 'int32')), 'float32') corr = K.sum(corr * mask, -1) / K.sum(mask, -1) return K.mean(corr)
def SSD_1_LOSS(self,yGT, yPred): # convert to matrix yGT = K.reshape(yGT,[20,5]) # GT has a fixed size, so we keep only non zero box yGT = self.get_y(yGT) # split BBox and Class bbox_GT = yGT[:,1:5] class_GT = yGT[:,0] # split class and box yPredClas = yPred[:self.nb_activation*self.nb_class] yPredBox = yPred[self.nb_activation*self.nb_class:] class_Pred = K.reshape(yPredClas,[self.nb_activation,self.nb_class]) bbox_Pred = K.reshape(yPredBox,[self.nb_activation,4]) # convert to activation a_ic = self.actn_to_bb(bbox_Pred) # ccompute jaccard matrix overlaps = self.jaccard(bbox_GT, self.anchor_cnr) # Map to ground truth gt_idx = K.argmax(overlaps,0) # [16] for each activation, ID of the GT with the best overlapp gt_overlap = K.max(overlaps,0) # [16] for each cell, ID of the GT with the best overlapp prior_idx = K.argmax(overlaps,1) # [4] for each GT, ID of best anchors prior_overlap = K.max(overlaps,1) # [4] for each GT, value of tye best overlapp # BBOX Loss ADD = tf.one_hot(prior_idx,self.nb_activation) ADD = K.cast(K.sum(ADD,axis=0),('float64')) gt_overlap = gt_overlap+ADD Threshold = 0.4 valid_anchor = gt_overlap>Threshold mask = K.cast(valid_anchor,('float32')) bbox = K.gather(bbox_GT,gt_idx) bbox = K.cast(bbox,('float32')) loc_loss = K.abs(a_ic-bbox) loc_loss = K.sum(loc_loss,axis=1) loc_loss = tf.multiply(loc_loss,mask) loc_loss = (K.sum(loc_loss))/K.sum(mask) # Classification Loss # Loss for overlapp >0.5 sel_gt_clas = K.gather(class_GT,gt_idx) gt_class_per_activation = K.one_hot(K.cast(sel_gt_clas,('int32')), 20) # for overlapp below 0.5, we have to put 0 in gt_class_per_activation valid_anchor = gt_overlap>Threshold mask = K.cast(valid_anchor,('float32')) mask = K.reshape(K.repeat_elements(mask,self.nb_class,0),(self.nb_activation,self.nb_class)) One_Hot_Overlap = gt_class_per_activation*mask # then, we estimate BCE for mandatory box (GT) pred_mandatory_anchor = K.gather(class_Pred,prior_idx) One_Hot_mandatory = tf.one_hot(K.cast(class_GT,'int32'),self.nb_class) target=K.concatenate([One_Hot_Overlap,One_Hot_mandatory],axis=0) pred=K.concatenate([class_Pred,pred_mandatory_anchor],axis=0) clas_loss = K.mean(K.binary_crossentropy(target, pred)) return clas_loss*5 + loc_loss
def get_motifs(model, X, Y, output_dir, output_dir2, label): layer_names = [l.name for l in model.layers] conv_layer_index = layer_names.index('conv1d_1') conv_layer = model.layers[conv_layer_index] num_motifs = conv_layer.filters window = conv_layer.kernel_size[0] conv_output = conv_layer.get_output_at(0) f = K.function([model.input], [K.max(K.max(conv_output, axis=1), axis=0)]) f_seq = K.function( [model.input], [K.argmax(conv_output, axis=1), K.max(conv_output, axis=1)]) f_act = K.function([model.input], [conv_output]) motifs = np.zeros((num_motifs, window, 4)) nsites = np.zeros(num_motifs) nseqs = np.zeros(num_motifs) Y_pos = [i for i, e in enumerate(Y) if e == label] X_pos = X[Y_pos] nsamples = len(X_pos) mean_acts = np.zeros((num_motifs, nsamples)) z = f([X_pos]) max_motif = z[0] thr_per = 0.5 z_seq = f_seq([X_pos]) max_inds = z_seq[0] max_acts = z_seq[1] z_act = f_act([X_pos]) acts = z_act[0] for m in range(num_motifs): for n in range(len(X_pos)): if max_acts[n, m] > thr_per * max_motif[m]: nseqs[m] += 1 ##get the filter activity and locations on the input sequence act_file = open(output_dir + 'motifs' + str(label) + '_act', 'w') loc_file = open(output_dir + 'motifs' + str(label) + '_loc', 'w') for m in range(num_motifs): for n in range(len(X_pos)): for j in range(acts.shape[1]): weight = (519 - abs(j - 519)) / 519 mean_acts[m, n] += acts[n, j, m] * weight if acts[n, j, m] > thr_per * max_motif[m]: nsites[m] += 1 motifs[m] += X_pos[n, j:j + window, :] loc_file.write("M%i %i %i\n" % (m, j, j + window)) for m in range(num_motifs): act_file.write("M%i" % (m)) for n in range(len(X_pos)): act_file.write("\t%0.4f" % (mean_acts[m, n])) act_file.write("\n") for m in range(num_motifs): seqfile = open(output_dir2 + 'motif' + str(m) + '.fasta', 'w') for n in range(len(X_pos)): for j in range(acts.shape[1]): if acts[n, j, m] > thr_per * max_motif[m]: nsites[m] += 1 motifs[m] += X_pos[n, j:j + window, :] kmer = one_hot_to_seq(X_pos[n, j:j + window, :]) seqfile.write('>%d_%d' % (n, j)) seqfile.write('\n') seqfile.write(kmer) seqfile.write('\n') print('Making motifs') motifs = motifs[:, :, [0, 3, 2, 1]] motifs_file = open(output_dir + 'motifs' + str(label) + '.txt', 'w') motifs_file.write( 'MEME version 4.9.0\n\n' 'ALPHABET= ACGT\n\n' 'strands: + -\n\n' 'Background letter frequencies (from uniform background):\n' 'A 0.25000 C 0.25000 G 0.25000 T 0.25000\n\n') for m in range(num_motifs): if nsites[m] == 0: continue motifs_file.write('MOTIF M%i O%i\n' % (m, m)) motifs_file.write( "letter-probability matrix: alength= 4 w= %i nsites= %i nseqs= %i E= 1337.0e-6\n" % (window, nsites[m], nseqs[m])) for j in range(window): motifs_file.write( "%f %f %f %f\n" % tuple(1.0 * motifs[m, j, 0:4] / np.sum(motifs[m, j, 0:4]))) motifs_file.write('\n') return num_motifs
def segnet_arch_2c_rgb(h, w): print("Model of size: %d %d" % (h, w)) ch = 3 # 1 inputs = Input(shape=(ch, h , w)) ordering = 'th' # 'th': (ch, h, w), 'tf': (h, w, ch) # 0 1 2 3 4 5 6 7 8 dropouts = [0.37, 0.51, 0.34, 0.48, 1, 0.48, 0.28, 0.78, 0.8] conv1 = Convolution2D(8, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(inputs) print 'conv1', conv1.get_shape() conv1 = Convolution2D(8, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv1) print 'conv1.', conv1.get_shape() pool1 = MaxPooling2D(pool_size=(2, 2),dim_ordering=ordering)(conv1) pool1 = Dropout(dropouts[0])(pool1) print 'pool1', pool1.get_shape() conv2 = Convolution2D(16, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(pool1) print 'conv2', conv2.get_shape() conv2 = Convolution2D(16, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv2) print 'conv2.', conv2.get_shape() pool2 = MaxPooling2D(pool_size=(2, 2),dim_ordering=ordering)(conv2) pool2 = Dropout(dropouts[1])(pool2) print 'pool2', pool2.get_shape() conv3 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(pool2) print 'conv3', conv3.get_shape() conv3 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv3) print 'conv3.', conv3.get_shape() pool3 = MaxPooling2D(pool_size=(2, 2),dim_ordering=ordering)(conv3) pool3 = Dropout(dropouts[2])(pool3) #changed from 0.4 to 0.25 print 'pool3', pool3.get_shape() conv4 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(pool3) print 'conv4', conv4.get_shape() conv4 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv4) print 'conv4.', conv4.get_shape() pool4 = MaxPooling2D(pool_size=(2, 2),dim_ordering=ordering)(conv4) pool4 = Dropout(dropouts[3])(pool4) #changed from 0.5 to 0.25 print 'pool4', pool4.get_shape() conv5 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(pool4) print 'conv5', conv5.get_shape() conv5 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv5) print 'conv5.', conv5.get_shape() up1 = UpSampling2D(size=(2, 2),dim_ordering=ordering)(conv5) print 'up1 upsampling2D:', up1.get_shape() up1 = merge([up1, conv4], mode='concat', concat_axis=1) #up1 = merge([(UpSampling2D(size=(2, 2),dim_ordering=ordering)(conv5)), pool4], mode='concat', concat_axis=1) up1 = Dropout(dropouts[4])(up1) print 'up1 merge conv4', up1.get_shape() conv8 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(up1) print 'conv8', conv8.get_shape() conv8 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv8) print 'conv8.', conv8.get_shape() up2 = UpSampling2D(size=(2, 2),dim_ordering=ordering)(conv8) print 'up2 upsampling2D:', up2.get_shape() up2 = merge([up2, conv3], mode='concat', concat_axis=1) #up2 = merge([UpSampling2D(size=(2, 2))(conv8), conv3], mode='concat', concat_axis=1) up2 = Dropout(dropouts[5])(up2) print 'up2 merge conv3',up2.get_shape() conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(up2) print 'conv9',conv9.get_shape() # 7,80,32 conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv9) print 'conv9.',conv9.get_shape() # 7,80,32 up3 = UpSampling2D(size=(2, 2),dim_ordering=ordering)(conv9) # 14, 160, 32 print 'up3 upsampling2D:', up3.get_shape() up3 = merge([up3, conv2], mode='concat', concat_axis=1) #up3 = merge([UpSampling2D(size=(2, 2))(conv9), conv2], mode='concat', concat_axis=1) up3 = Dropout(dropouts[6])(up3) print 'up3 merge conv2',up3.get_shape() conv10 = Convolution2D(16, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(up3) print 'conv10',conv10.get_shape() conv10 = Convolution2D(16, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv10) print 'conv10.',conv10.get_shape() up4 = UpSampling2D(size=(2, 2),dim_ordering=ordering)(conv10) print 'up4 upsampling2D:', up4.get_shape() up4 = merge([up4, conv1], mode='concat', concat_axis=1) #up4 = merge([UpSampling2D(size=(2, 2))(conv10), conv1], mode='concat', concat_axis=1) up4 = Dropout(dropouts[7])(up4) print 'up4 merge conv1',up4.get_shape() conv11 = Convolution2D(8, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(up4) print 'conv11',conv11.get_shape() conv11 = Convolution2D(8, 3, 3, activation='relu', border_mode='same', init='he_normal',dim_ordering=ordering)(conv11) print 'conv11.',conv11.get_shape() #conv12 = Convolution2D(ch, 1, 1, activation='sigmoid', init='he_normal',dim_ordering=ordering)(conv11) conv12 = Convolution2D(1, 1, 1, activation='sigmoid', init='he_normal',dim_ordering=ordering)(conv11) print 'out',conv12.get_shape() predictions = K.argmax(conv12, axis=1) model = Model(input=inputs, output=[conv12]) model.summary() #return model return model, predictions