def add_n_bottleneck_blocks(in_layer, n_outfmaps, n_repeat, is_first_layer): assert n_outfmaps % 4 == 0 a0 = in_layer for i1 in xrange(n_repeat): if i1 == 0: if is_first_layer is True: strides = (1, 1) shortcut = a0 else: strides = (2, 2) shortcut = Convolution2D(n_outfmaps=n_outfmaps, n_row=1, n_col=1, act='linear', border_mode='valid', strides=strides)(a0) else: strides = (1, 1) shortcut = a0 a1 = BN(axes=(0, 2, 3))(a0) a2 = Activation('relu')(a1) a3 = Convolution2D(n_outfmaps=n_outfmaps, n_row=1, n_col=1, act='linear', border_mode='valid', strides=strides)(a2) a4 = BN(axes=(0, 2, 3))(a3) a5 = Activation('relu')(a4) a6 = Convolution2D(n_outfmaps=n_outfmaps / 4, n_row=3, n_col=3, act='linear', border_mode=(1, 1), strides=(1, 1))(a5) a7 = BN(axes=(0, 2, 3))(a6) a8 = Activation('relu')(a7) a9 = Convolution2D(n_outfmaps=n_outfmaps, n_row=1, n_col=1, act='linear', border_mode='valid', strides=(1, 1))(a8) a10 = Lambda(add_layers)([shortcut, a9]) a0 = a10 return a0
def add_n_blocks(in_layer, n_outfmaps, n_repeat, is_first_layer): a0 = in_layer for i1 in range(n_repeat): if i1 == 0: if is_first_layer is True: strides = (1, 1) shortcut = a0 else: strides = (2, 2) shortcut = Convolution2D(n_outfmaps=n_outfmaps, n_row=1, n_col=1, act='linear', border_mode='valid', strides=strides)(a0) else: strides = (1, 1) shortcut = a0 a1 = Convolution2D(n_outfmaps=n_outfmaps, n_row=3, n_col=3, act='linear', border_mode=(1, 1), strides=strides)(a0) a2 = BN(axes=(0, 2, 3))(a1) a3 = Activation('relu')(a2) a4 = Convolution2D(n_outfmaps=n_outfmaps, n_row=3, n_col=3, act='linear', border_mode=(1, 1), strides=(1, 1))(a3) a5 = BN(axes=(0, 2, 3))(a4) a6 = Activation('relu')(a5) a7 = Lambda(add_layers)([shortcut, a6]) a0 = a7 return a0
def add_n_blocks(seq, n_outfmaps, n_repeat, is_first_layer): for i1 in range(n_repeat): if i1 == 0: if is_first_layer is True: strides = (1, 1) else: strides = (2, 2) else: strides = (1, 1) seq.add( Convolution2D(n_outfmaps=n_outfmaps, n_row=3, n_col=3, act='linear', border_mode=(1, 1), strides=strides)) seq.add(BN(axes=(0, 2, 3))) seq.add(Activation('relu')) return seq
def train(): # load data batch_size = 128 tr_X, tr_y, va_X, va_y, te_X, te_y = pp_data.load_data() n_batches = int(tr_X.shape[0] / batch_size) # normalize data between [-1,1] tr_X = (tr_X - 0.5) * 2 tr_X = tr_X.reshape((50000, 1, 28, 28)) print tr_X.shape # generator a0 = InputLayer(100) a1 = Dense(128 * 7 * 7, act='linear')(a0) a1 = BN(axis=0)(a1) a1 = Reshape(out_shape=(128, 7, 7))(a1) a1 = Convolution2D(64, 5, 5, act='linear', border_mode=(2, 2))(a1) a1 = BN(axis=(0, 2, 3))(a1) a1 = Activation('leaky_relu')(a1) a1 = UpSampling2D(size=(2, 2))(a1) a1 = Convolution2D(32, 5, 5, act='linear', border_mode=(2, 2))(a1) a1 = BN(axis=(0, 2, 3))(a1) a1 = Activation('leaky_relu')(a1) a1 = UpSampling2D(size=(2, 2))(a1) a8 = Convolution2D(1, 5, 5, act='tanh', border_mode=(2, 2), name='a8')(a1) g = Model([a0], [a8]) g.compile() g.summary() # discriminator b0 = InputLayer((1, 28, 28), name='b0') b1 = Convolution2D(64, 5, 5, act='relu', border_mode=(0, 0), name='b1')(b0) b1 = MaxPooling2D(pool_size=(2, 2))(b1) b1 = Convolution2D(128, 5, 5, act='relu', border_mode=(0, 0))(b1) b1 = MaxPooling2D(pool_size=(2, 2))(b1) b1 = Flatten()(b1) b8 = Dense(1, act='sigmoid')(b1) d = Model([b0], [b8]) d.compile() d.summary() # discriminator on generator d_on_g = Model() d.set_trainability(False) d_on_g.add_models([g, d]) d.set_trainability(True) d_on_g.joint_models('a8', 'b0') d_on_g.compile() d_on_g.summary() # optimizer opt_d = Adam(1e-4) opt_g = Adam(1e-4) # optimization function f_train_d = d.get_optimization_func(target_dims=[2], loss_func='binary_crossentropy', optimizer=opt_d, clip=None) f_train_g = d_on_g.get_optimization_func(target_dims=[2], loss_func='binary_crossentropy', optimizer=opt_g, clip=None) noise = np.zeros((batch_size, 100)) for epoch in range(100): print epoch for index in range(n_batches): # concatenate generated img and real image to train discriminator. noise = np.random.uniform(-1, 1, (batch_size, 100)) batch_x = tr_X[index * batch_size:(index + 1) * batch_size] batch_gx = g.predict(noise) batch_x_all = np.concatenate((batch_x, batch_gx)) # assign real img label as 1, generated img label as 0 batch_y_all = np.array([1] * batch_size + [0] * batch_size) batch_y_all = batch_y_all.reshape((batch_y_all.shape[0], 1)) # save out generated img if index % 50 == 0: image = pp_data.combine_images(batch_gx) image = image * 127.5 + 127.5 if not os.path.exists("img_dcgan"): os.makedirs("img_dcgan") Image.fromarray(image.astype( np.uint8)).save("img_dcgan/" + str(epoch) + "_" + str(index) + ".png") # train discriminator d_loss = d.train_on_batch(f_train_d, batch_x_all, batch_y_all) # assign generate img label as 1, so as to deceive discriminator noise = np.random.uniform(-1, 1, (batch_size, 100)) batch_y_all = np.array([1] * batch_size) batch_y_all = batch_y_all.reshape((batch_y_all.shape[0], 1)) # train generator g_loss = d_on_g.train_on_batch(f_train_g, noise, batch_y_all) print index, "d_loss:", d_loss, "\tg_loss:", g_loss
def train(args): workspace = cfg.workspace te_fold = cfg.te_fold n_events = args.n_events snr = args.snr feature_dir = os.path.join(workspace, "features", "logmel", "n_events=%d" % n_events) yaml_dir = os.path.join(workspace, "mixed_audio", "n_events=%d" % n_events) (tr_x, tr_at_y, tr_sed_y, tr_na_list, te_x, te_at_y, te_sed_y, te_na_list) = pp_data.load_data(feature_dir=feature_dir, yaml_dir=yaml_dir, te_fold=te_fold, snr=snr, is_scale=is_scale) print(tr_x.shape, tr_at_y.shape) print(te_x.shape, te_at_y.shape) (_, n_time, n_freq) = tr_x.shape n_out = len(cfg.events) if False: for e in tr_x: plt.matshow(e.T, origin='lower', aspect='auto') plt.show() # Build model. lay_in = InputLayer(in_shape=(n_time, n_freq)) a = Reshape((1, n_time, n_freq))(lay_in) a = Conv2D(n_outfmaps=64, n_row=3, n_col=5, act='linear', strides=(1, 1), border_mode=(1, 2))(a) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = Conv2D(n_outfmaps=64, n_row=3, n_col=5, act='linear', strides=(1, 1), border_mode=(1, 2))(a) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = Dropout(p_drop=0.2)(a) a = Conv2D(n_outfmaps=64, n_row=3, n_col=5, act='linear', strides=(1, 1), border_mode=(1, 2))(a) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = Conv2D(n_outfmaps=64, n_row=3, n_col=5, act='linear', strides=(1, 1), border_mode=(1, 2))(a) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = Dropout(p_drop=0.2)(a) a = Conv2D(n_outfmaps=64, n_row=3, n_col=5, act='linear', strides=(1, 1), border_mode=(1, 2))(a) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = Conv2D(n_outfmaps=64, n_row=3, n_col=5, act='linear', strides=(1, 1), border_mode=(1, 2))(a) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = Dropout(p_drop=0.2)(a) a = Conv2D(n_outfmaps=n_out, n_row=1, n_col=1, act='sigmoid', border_mode=(0, 0), name='seg_masks')(a) a8 = Lambda(_global_avg_pooling, name='a8')(a) md = Model([lay_in], [a8]) md.compile() md.summary(is_logging=True) # Callbacks. md_dir = os.path.join(workspace, "models", pp_data.get_filename(__file__), "n_events=%d" % n_events, "fold=%d" % te_fold, "snr=%d" % snr) pp_data.create_folder(md_dir) save_model = SaveModel(md_dir, call_freq=50, type='iter', is_logging=True) validation = Validation(te_x=te_x, te_y=te_at_y, batch_size=50, call_freq=50, metrics=['binary_crossentropy'], dump_path=None, is_logging=True) callbacks = [save_model, validation] observe_nodes = [md.find_layer('seg_masks').output_] f_forward = md.get_observe_forward_func(observe_nodes) # Generator. tr_gen = DataGenerator(batch_size=32, type='train') eva_gen = DataGenerator2(batch_size=32, type='test') # Train. loss_ary = [] t1 = time.time() optimizer = Adam(1e-3) for (batch_x, batch_y) in tr_gen.generate(xs=[tr_x], ys=[tr_at_y]): if md.iter_ % 50 == 0: logging.info("iter: %d tr_loss: %f time: %s" % ( md.iter_, np.mean(loss_ary), time.time() - t1, )) t1 = time.time() loss_ary = [] # if md.iter_ % 200 == 0: # write_out_at_sed(md, eva_gen, f_forward, te_x, te_at_y, te_sed_y, n_events, snr, te_fold) if md.iter_ == 5001: break loss = md.train_on_batch(batch_x, batch_y, loss_func='binary_crossentropy', optimizer=optimizer, callbacks=callbacks) loss_ary.append(loss)
tr_y = sparse_to_categorical(tr_y, n_out) te_y = sparse_to_categorical(te_y, n_out) print tr_X.shape print tr_y.shape ### Build model seq = Sequential() seq.add(InputLayer(in_shape=(3, 32, 32))) seq.add( Convolution2D(n_outfmaps=64, n_row=3, n_col=3, act='linear', border_mode=(1, 1))) seq.add(BN(axes=(0, 2, 3))) seq.add(Activation('relu')) seq.add( Convolution2D(n_outfmaps=64, n_row=3, n_col=3, act='linear', border_mode=(1, 1))) seq.add(BN(axes=(0, 2, 3))) seq.add(Activation('relu')) seq.add(MaxPool2D(pool_size=(2, 2))) seq.add( Convolution2D(n_outfmaps=128, n_row=3, n_col=3,
# init params n_in = 784 n_hid = 500 n_out = 10 # sparse label to 1-of-K categorical label tr_y = sparse_to_categorical(tr_y, n_out) va_y = sparse_to_categorical(va_y, n_out) te_y = sparse_to_categorical(te_y, n_out) ### Build model lay_in = InputLayer(in_shape=(1, 28, 28)) a = Conv2D(n_outfmaps=32, n_row=3, n_col=3, act='linear', strides=(1, 1), border_mode=(1, 1))(lay_in) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = MaxPooling2D(pool_size=(2, 2))(a) a = Dropout(p_drop=0.2)(a) a = Conv2D(n_outfmaps=64, n_row=3, n_col=3, act='linear', strides=(1, 1), border_mode=(1, 1))(a) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = MaxPooling2D(pool_size=(2, 2))(a) a = Dropout(p_drop=0.2)(a) a = Conv2D(n_outfmaps=128, n_row=3, n_col=3, act='linear', strides=(1, 1), border_mode=(1, 1))(a) a = BN(axis=(0, 2, 3))(a) a = Activation('relu')(a) a = MaxPooling2D(pool_size=(2, 2))(a) a = Dropout(p_drop=0.2)(a)