def cnn(X, y): nb_classes = y.shape.as_list()[-1] with N.args_scope(['Conv', dict(b_init=None, activation=K.linear)], ['BatchNorm', dict(activation=K.relu)]): f = N.Sequence([ N.Dimshuffle(pattern=(0, 2, 3, 1)), N.Conv(32, (3, 3), pad='same', stride=(1, 1)), N.BatchNorm(), N.Conv(32, (3, 3), pad='same', stride=(1, 1), b_init=0, activation=K.relu), N.Pool(pool_size=(2, 2), strides=None, mode='max'), N.Dropout(level=0.25), # N.Conv(64, (3, 3), pad='same', stride=(1, 1)), N.BatchNorm(), N.Conv(64, (3, 3), pad='same', stride=(1, 1), b_init=0., activation=K.relu), N.Pool(pool_size=(2, 2), strides=None, mode='max'), N.Dropout(level=0.25), # N.Flatten(outdim=2), N.Dense(512, activation=K.relu), N.Dropout(level=0.5), N.Dense(nb_classes, activation=K.linear) ], debug=1) logit = f(X) prob = tf.nn.softmax(logit) return {'logit': logit, 'prob': prob}
X_samples, y_samples = X_train[:25], y_train[:25] input_shape = ds['X_train'].shape input_ndim = len(input_shape) print("Train shape:", ctext(X_train.shape, 'cyan')) print("Valid shape:", ctext(X_valid.shape, 'cyan')) print("Test shape:", ctext(X_test.shape, 'cyan')) # ====== create basic tensor ====== # X = K.placeholder(shape=(None,) + input_shape[1:], name='X_input') y = K.placeholder(shape=(None,), name='y_input') # =========================================================================== # Create the network # =========================================================================== LATENT_DROPOUT = 0.3 if args.cnn: with N.args_scope(([N.Conv, N.Dense], dict(b_init=None, activation=K.linear)), (N.BatchNorm, dict(activation=tf.nn.elu)), (N.Pool, dict(mode='max', pool_size=2))): f_encoder = N.Sequence([ N.Dropout(level=0.5), N.Dimshuffle((0, 2, 3, 1)) if is_cifar10 else N.Dimshuffle((0, 1, 2, 'x')), N.Conv(num_filters=32, filter_size=3, pad='valid'), N.Pool(), N.BatchNorm(), N.Conv(num_filters=64, filter_size=3, pad='same'), N.BatchNorm(), N.Conv(num_filters=64, filter_size=3, pad='valid'), N.BatchNorm(activation=tf.nn.elu), N.Pool(),
n_classes = len(labels) # =========================================================================== # Create model # =========================================================================== inputs = [ K.placeholder(shape=(None, ) + shape[1:], dtype='float32', name='input%d' % i) for i, shape in enumerate(as_tuple_of_shape(train.shape)) ] X = inputs[0] y = inputs[1] print("Inputs:", ctext(inputs, 'cyan')) # ====== create the networks ====== # with N.args_scope([('Conv', 'Dense'), dict(b_init=None, activation=K.linear, pad='same')], ['BatchNorm', dict(activation=K.relu)]): f = N.Sequence([ N.Dimshuffle(pattern=(0, 1, 2, 'x')), N.Conv(num_filters=32, filter_size=(9, 7)), N.BatchNorm(), N.Pool(pool_size=(3, 2), strides=2), N.Conv(num_filters=64, filter_size=(5, 3)), N.BatchNorm(), N.Pool(pool_size=(3, 1), strides=(2, 1), name='PoolOutput1'), N.Conv(num_filters=64, filter_size=(5, 3)), N.BatchNorm(), N.Pool(pool_size=(3, 2), strides=(2, 2), name='PoolOutput2'), N.Flatten(outdim=2), N.Dense(512, name="LatentDense"), N.BatchNorm(),
X_samples, y_samples = X_train[:25], y_train[:25] input_shape = ds['X_train'].shape input_ndim = len(input_shape) print("Train shape:", ctext(X_train.shape, 'cyan')) print("Valid shape:", ctext(X_valid.shape, 'cyan')) print("Test shape:", ctext(X_test.shape, 'cyan')) # ====== create basic tensor ====== # X = K.placeholder(shape=(None,) + input_shape[1:], name='X_input') y = K.placeholder(shape=(None,), name='y_input') # =========================================================================== # Create the network # =========================================================================== LATENT_DROPOUT = 0.3 if args.cnn: with N.args_scope(([N.Conv, N.Dense], dict(b_init=None, activation=K.linear)), (N.BatchNorm, dict(activation=tf.nn.elu)), (N.Pool, dict(mode='max', pool_size=2))): f_encoder = N.Sequence([ N.Dropout(level=0.5), N.Dimshuffle((0, 2, 3, 1)) if is_cifar10 else N.Dimshuffle((0, 1, 2, 'x')), N.Conv(num_filters=32, filter_size=3, pad='valid'), N.Pool(), N.BatchNorm(), N.Conv(num_filters=64, filter_size=3, pad='same'), N.BatchNorm(), N.Conv(num_filters=64, filter_size=3, pad='valid'), N.BatchNorm(activation=tf.nn.elu), N.Pool(),
K.placeholder(shape=(None, ) + shape[1:], dtype='float32', name='input%d' % i) for i, shape in enumerate(as_tuple_of_shape(train.shape)) ] X = inputs[0] y = inputs[1] print("Inputs:", ctext(inputs, 'cyan')) # ====== the network ====== # if os.path.exists(MODEL_PATH): x_vec = N.deserialize(path=MODEL_PATH, force_restore_vars=True) else: TRAIN_MODEL = True with N.args_scope( ['TimeDelayedConv', dict(time_pool='none', activation=K.relu)], ['Dense', dict(activation=K.linear, b_init=None)], ['BatchNorm', dict(activation=K.relu)]): x_vec = N.Sequence([ N.Dropout(level=0.3), N.TimeDelayedConv(n_new_features=512, n_time_context=5), N.TimeDelayedConv(n_new_features=512, n_time_context=5), N.TimeDelayedConv(n_new_features=512, n_time_context=7), N.Dense(512), N.BatchNorm(), N.Dense(1500), N.BatchNorm(), N.StatsPool(axes=1, output_mode='concat'), N.Flatten(outdim=2), N.Dense(512, name="LatentOutput"), N.BatchNorm(),
# =========================================================================== inputs = [K.placeholder(shape=(None,) + shape[1:], dtype='float32', name='input%d' % i) for i, shape in enumerate(as_tuple_of_shape(train.shape))] X = inputs[0] y = inputs[1] print("Inputs:", ctext(inputs, 'cyan')) # ====== the network ====== # if os.path.exists(MODEL_PATH): x_vec = N.deserialize(path=MODEL_PATH, force_restore_vars=True) else: TRAIN_MODEL = True with N.args_scope( ['TimeDelayedConv', dict(time_pool='none', activation=K.relu)], ['Dense', dict(activation=K.linear, b_init=None)], ['BatchNorm', dict(activation=K.relu)] ): x_vec = N.Sequence([ N.Dropout(level=0.3), N.TimeDelayedConv(n_new_features=512, n_time_context=5), N.TimeDelayedConv(n_new_features=512, n_time_context=5), N.TimeDelayedConv(n_new_features=512, n_time_context=7), N.Dense(512), N.BatchNorm(), N.Dense(1500), N.BatchNorm(), N.StatsPool(axes=1, output_mode='concat'), N.Flatten(outdim=2),
# ====== load data feeder ====== # (train, valid, X_test_name, X_test_true, X_test_data, labels) = prepare_data(feat=args.feat, label=args.task) n_classes = len(labels) # =========================================================================== # Create model # =========================================================================== inputs = [K.placeholder(shape=(None,) + shape[1:], dtype='float32', name='input%d' % i) for i, shape in enumerate(as_tuple_of_shape(train.shape))] X = inputs[0] y = inputs[1] print("Inputs:", ctext(inputs, 'cyan')) # ====== create the networks ====== # with N.args_scope( [('Conv', 'Dense'), dict(b_init=None, activation=K.linear, pad='same')], ['BatchNorm', dict(activation=K.relu)]): f = N.Sequence([ N.Dimshuffle(pattern=(0, 1, 2, 'x')), N.Conv(num_filters=32, filter_size=(9, 7)), N.BatchNorm(), N.Pool(pool_size=(3, 2), strides=2), N.Conv(num_filters=64, filter_size=(5, 3)), N.BatchNorm(), N.Pool(pool_size=(3, 1), strides=(2, 1), name='PoolOutput1'), N.Conv(num_filters=64, filter_size=(5, 3)), N.BatchNorm(), N.Pool(pool_size=(3, 2), strides=(2, 2), name='PoolOutput2'), N.Flatten(outdim=2), N.Dense(512, name="LatentDense"), N.BatchNorm(), N.Dense(512), N.BatchNorm(),