예제 #1
0
x_valid, y_valid = x_train[-10000:], y_train[-10000:]
x_train, y_train = x_train[:-10000], y_train[:-10000]

print(x_train.shape, y_train.shape, x_valid.shape, y_valid.shape, x_test.shape,
      y_test.shape)

# retrieve image and label shapes from training data
img_rows, img_cols, img_chans = x_train.shape[1:]
n_classes = np.unique(y_train).shape[0]

print(img_rows, img_cols, img_chans, n_classes)

# convert labels to 1-hot vectors
y_train = tf.keras.utils.to_categorical(y_train, n_classes)
y_valid = tf.keras.utils.to_categorical(y_valid, n_classes)
y_test = tf.keras.utils.to_categorical(y_test, n_classes)

print(y_train.shape, y_valid.shape, y_test.shape)

# normalize inputs and cast to float
x_train = (x_train / np.max(x_train)).astype(np.float32)
x_valid = (x_valid / np.max(x_valid)).astype(np.float32)
x_test = (x_test / np.max(x_test)).astype(np.float32)

nn = ConvNet(img_rows, img_cols, img_chans, n_classes)
nn.maybe_train(data_train=(x_train, y_train),
               data_valid=(x_valid, y_valid),
               batch_size=16,
               epochs=12)
nn.evaluate(x_train, y_train)
def train_cv(args):

    classes = sorted([
        'anxiety', 'baseline', 'concentration', 'digestion', 'disgust',
        'frustration'
    ])
    n_classes = len(classes)
    path = os.path.join(args.data_root, args.feats)

    cv_scores = {}
    cv_scores['train'] = []
    cv_scores['test'] = []
    sub_dirs = np.array(os.listdir(path))
    n_splits = 10
    kfold = KFold(n_splits=n_splits, shuffle=True, random_state=0)
    for i, (train, test) in enumerate(kfold.split(sub_dirs)):
        print('Fold {} of {}'.format(i + 1, n_splits))
        X_train, y_train = path_label_helper(args, sub_dirs[train])
        X_test, y_test = path_label_helper(args, sub_dirs[test])

        sample = np.load(os.path.join(X_train[0]))

        if args.model is 'CNN':
            input_shape = (sample.shape[0] * args.total_dur, sample.shape[1],
                           1)
            model = ConvNet(input_shape=input_shape)

        elif args.model is 'RCNN':
            feat_dim = int(args.delta_time * 100)
            time_dim = int(args.total_dur * sample.shape[0] / feat_dim)
            # (10, 30, 128, 1)
            input_shape = (time_dim, feat_dim, sample.shape[1], 1)
            model = Recurrent2DConvNet(input_shape=input_shape)

        tg = DataGenerator(paths=X_train,
                           targets=y_train,
                           mode=args.model,
                           td=args.total_dur,
                           dt=args.delta_time,
                           n_classes=n_classes,
                           input_shape=input_shape)

        vg = DataGenerator(paths=X_test,
                           targets=y_test,
                           mode=args.model,
                           td=args.total_dur,
                           dt=args.delta_time,
                           n_classes=n_classes,
                           input_shape=input_shape)

        class_weight = compute_class_weight('balanced', np.unique(y_train),
                                            y_train)

        model.fit_generator(generator=tg,
                            epochs=args.epochs,
                            verbose=1,
                            class_weight=class_weight)

        train_scores = model.evaluate(tg)
        test_scores = model.evaluate(vg)
        cv_scores['train'].append(train_scores[1])
        cv_scores['test'].append(test_scores[1])

    print(cv_scores)

    with open(os.path.join('results', 'cv_scores.pkl'), 'wb') as handle:
        pickle.dump(cv_scores, handle, protocol=pickle.HIGHEST_PROTOCOL)