from tensorflow.contrib.keras.python.keras import backend as K from tensorflow.contrib.keras.python.keras import losses x = np.random.random((10, 10)) * 2 y = np.random.randint(2, size=(10, 1)) input_tensor = Input(shape=(10, )) bn_tensor = BatchNormalization()(input_tensor) dp_tensor = Dropout(0.7)(bn_tensor) final_tensor = Dense(1)(dp_tensor) model = Model(input_tensor, final_tensor) model.compile(optimizer='SGD', loss='mse') # x, y won't get into batches for training here loss_on_batch = model.train_on_batch(x, y) # dive in for details """ ('Runs a single gradient update on a single batch of data.\n' '\n' 'Arguments:\n' ' x: Numpy array of training data,\n' ' or list of Numpy arrays if the model has multiple inputs.\n' ' If all inputs in the model are named,\n' ' you can also pass a dictionary\n' ' mapping input names to Numpy arrays.\n' ' y: Numpy array of target data,\n' ' or list of Numpy arrays if the model has multiple outputs.\n' ' If all outputs in the model are named,\n' ' you can also pass a dictionary\n' ' mapping output names to Numpy arrays.\n' ' sample_weight: optional array of the same length as x, containing\n'
if len(rpn_accuracy_rpn_monitor) == epoch_length and C.verbose: mean_overlapping_bboxes = float( sum(rpn_accuracy_rpn_monitor)) / len(rpn_accuracy_rpn_monitor) rpn_accuracy_rpn_monitor = [] dprint( 'Average number of overlapping bounding boxes from RPN = {} for {} previous iterations' .format(mean_overlapping_bboxes, epoch_length)) if mean_overlapping_bboxes == 0: dprint( 'RPN is not producing bounding boxes that overlap the ground truth boxes. Results will not be satisfactory. Keep training.' ) X, Y, img_data = next(data_gen_train) loss_rpn = model_rpn.train_on_batch(X, Y) P_rpn = model_rpn.predict_on_batch(X) R = roi_helpers.rpn_to_roi(P_rpn[0], P_rpn[1], C, use_regr=True, overlap_thresh=0.7, max_boxes=300) # note: calc_iou converts from (x1,y1,x2,y2) to (x,y,w,h) format X2, Y1, Y2 = roi_helpers.calc_iou(R, img_data, C, class_mapping) if X2 is None: rpn_accuracy_rpn_monitor.append(0)
def main(): training_images, training_labels, test_images, test_labels = load_dataset() # plt.imshow(training_images[:,:,0], cmap='gray') # plt.show() perm_train = np.random.permutation(training_labels.size) training_labels = training_labels[perm_train] training_images = (training_images[perm_train, :, :] - 127.5) / 127.5 training_images = np.expand_dims(training_images, -1) print(training_images.shape) test_images = test_images / 255.0 test_images = np.expand_dims(test_images, -1) # pdb.set_trace() # training_labels = to_categorical(training_labels, NUM_CLASSES) # test_labels = to_categorical(test_labels, NUM_CLASSES) BATCH_SIZE = 32 * 8 WIDTH, HEIGHT = 28, 28 adam_lr = 0.0002 adam_beta_1 = 0.5 ##################################### ### Defiining the Discriminator: ##################################### input_D = Input(shape=(HEIGHT, WIDTH, 1), name='input_D') x = Conv2D(filters=32, kernel_size=3, strides=(2, 2), padding='same', name='conv1_D')(input_D) #x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(filters=32, kernel_size=3, strides=(2, 2), padding='same', name='conv2_D')(x) #x = BatchNormalization()(x) x = Activation('relu')(x) x = Flatten()(x) x = Dense(128, activation='relu', name='dense1_D')(x) output_D = Dense(1, activation='sigmoid', name='output_D')(x) model_D = Model(inputs=input_D, outputs=output_D) model_D.compile(loss='binary_crossentropy', optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr, beta1=adam_beta_1), metrics=['accuracy']) ##################################### ### Defiining the Generator: ##################################### LATENT_SIZE = 100 input_G = Input(shape=(LATENT_SIZE, ), name='input_gen') x = Dense(7 * 7 * 32, activation='linear', name='Dense1_G')(input_G) x = BatchNormalization()(x) x = Activation('relu')(x) x = Reshape((7, 7, 32))(x) x = UpSampling2D((2, 2))(x) x = Conv2D(filters=32, kernel_size=3, strides=(1, 1), padding='same', name='conv1_gen')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(filters=32, kernel_size=3, strides=(1, 1), padding='same', name='conv2_gen')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(filters=1, kernel_size=1, strides=(1, 1), padding='same', name='conv3_gen')(x) img_G = Activation('tanh')(x) model_G = Model(inputs=input_G, outputs=img_G) model_G.compile(loss='binary_crossentropy', optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr, beta1=adam_beta_1)) ##################################### ### Defiining the Combined GAN: ##################################### model_D.trainable = False # Since model_D is already compiled, thediscriminator model remains trainble, # but here in the combined model it becomes non-trainable input_main = Input( shape=(LATENT_SIZE, ), name='input_main' ) # Note that this input should be different from the input to Generator combined = Model(inputs=input_main, outputs=model_D(model_G(input_main))) combined.compile(loss='binary_crossentropy', optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr, beta1=adam_beta_1), metrics=['accuracy']) print(combined.summary()) ##################################### ### Training: ##################################### bar = InitBar() N = training_images.shape[0] for iter in range(100): fake_input = np.random.randn(1, LATENT_SIZE) fake_image = model_G.predict(fake_input) loss_G, acc_G, loss_D, acc_D = 0, 0, 0, 0 steps = (int)(np.ceil(float(N) / float(BATCH_SIZE))) for batch_iter in range(steps): bar(100.0 * batch_iter / float(steps)) real_image, _ = get_batch(batch_iter, BATCH_SIZE / 2, training_images, training_labels) #################### ## Discriminator Training #################### # Note that if using BN layer in Discriminator, minibatch should contain only real images or fake images. fake_input = np.random.randn(BATCH_SIZE / 2, LATENT_SIZE) fake_image = model_G.predict(fake_input) #real_image = get_real_mbatch(batch_sz=BATCH_SIZE/2, data=training_images) agg_input = np.concatenate((fake_image, real_image), axis=0) agg_output = np.zeros((BATCH_SIZE, )) agg_output[BATCH_SIZE / 2:] = 1 perm = np.random.permutation(BATCH_SIZE) agg_input = agg_input[perm] agg_output = agg_output[perm] #pdb.set_trace() tr = model_D.train_on_batch(x=agg_input, y=agg_output) loss_D += tr[0] acc_D += tr[1] ##################### ## Generator Training ##################### fake_input = np.random.randn(BATCH_SIZE, LATENT_SIZE) fake_label = np.ones(BATCH_SIZE, ) tr = combined.train_on_batch(x=fake_input, y=fake_label) loss_G += tr[0] acc_G += tr[1] print('\nG_loss = {}, G_acc = {}\nD_loss = {}, D_acc = {}'.format( loss_G / float(steps), acc_G / float(steps), loss_D / float(steps), acc_D / float(steps))) for iter in range(10): fake_input = np.random.randn(1, LATENT_SIZE) fake_image = model_G.predict(fake_input) plt.imshow(fake_image[0, :, :, 0]) plt.show()