def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir, use_mini_dataset)

    # Split into train and dev
    dev_split_index = int(9 * len(X_train) / 10)
    X_dev = X_train[dev_split_index:]
    y_dev = [y_train[0][dev_split_index:], y_train[1][dev_split_index:]]
    X_train = X_train[:dev_split_index]
    y_train = [y_train[0][:dev_split_index], y_train[1][:dev_split_index]]

    permutation = np.array([i for i in range(len(X_train))])
    np.random.shuffle(permutation)
    X_train = [X_train[i] for i in permutation]
    y_train = [[y_train[0][i] for i in permutation], [y_train[1][i] for i in permutation]]

    # Split dataset into batches
    train_batches = batchify_data(X_train, y_train, batch_size)
    dev_batches = batchify_data(X_dev, y_dev, batch_size)
    test_batches = batchify_data(X_test, y_test, batch_size)

    # Load model
    input_dimension = img_rows * img_cols
    model = CNN(input_dimension) # TODO add proper layers to CNN class above

    # Train
    train_model(train_batches, dev_batches, model)

    ## Evaluate the model on test data
    loss, acc = run_epoch(test_batches, model.eval(), None)
    print('Test loss1: {:.6f}  accuracy1: {:.6f}  loss2: {:.6f}   accuracy2: {:.6f}'.format(loss[0], acc[0], loss[1], acc[1]))
コード例 #2
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data

    #=================== Model ======================#
    # TO DO: Define a model with input the first image and outputs the two digits. The model variable should be called model and should be based on
    # a multilayer fully connected network. Consult https://keras.io/getting-started/functional-api-guide/ for the relevant API.
    input_layer = Input(shape=(1, 42, 28))
    flat_layer = Flatten()(input_layer)
    dense_layer = Dense(64, activation="relu")(flat_layer)
    output = Dense(num_classes, activation="softmax")(dense_layer)
    output_2 = Dense(num_classes, activation='softmax')(dense_layer)
    model = Model(input_layer, [output, output_2])
    model.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'],
                  loss_weights=[0.5, 0.5])
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)

    objective_score = model.evaluate(X_test, y_test, batch_size=64)
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #3
0
def main():
    print(f"Device: {device}")

    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir, use_mini_dataset)

    # Split into train and dev
    dev_split_index = int(9 * len(X_train) / 10)
    X_dev = X_train[dev_split_index:]
    y_dev = [y_train[0][dev_split_index:], y_train[1][dev_split_index:]]
    X_train = X_train[:dev_split_index]
    y_train = [y_train[0][:dev_split_index], y_train[1][:dev_split_index]]

    permutation = np.array([i for i in range(len(X_train))])
    np.random.shuffle(permutation)
    X_train = [X_train[i] for i in permutation]
    y_train = [[y_train[0][i] for i in permutation], [y_train[1][i] for i in permutation]]

    # Split dataset into batches
    train_batches = batch_data(X_train, y_train, batch_size)
    dev_batches = batch_data(X_dev, y_dev, batch_size)
    test_batches = batch_data(X_test, y_test, batch_size)

    # Load model
    input_dimension = img_rows * img_cols
    model = MLP(input_dimension).to(device)

    # Train
    train_model(train_batches, dev_batches, model, n_epochs=n_epochs)

    # Evaluate the model on test data
    loss, acc = run_epoch(test_batches, model.eval(), None)
    print(f'Test loss1: {loss[0]:.6f}  accuracy1: {acc[0]:.6f}  loss2: {loss[1]:.6f}   accuracy2: {acc[1]:.6f}')
コード例 #4
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)

    digit_input = Input(shape=(1, img_rows, img_cols))
    x = Flatten()(digit_input)
    x = Dense(64, activation='relu')(x)
    x = Dense(64, activation='relu')(x)
    main_output = Dense(10, activation='softmax', name='main_output')(x)
    auxiliary_output = Dense(10, activation='softmax',
                             name='auxiliary_output')(x)

    model = Model(input=digit_input, output=[main_output, auxiliary_output])

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'],
                  loss_weights=[0.5, 0.5])
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)
    objective_score = model.evaluate(X_test, [y_test[0], y_test[1]],
                                     batch_size=batch_size)  #TO  BE COMPLETED.
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    #model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #5
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir, use_mini_dataset)

    digit_input = Input(shape=(1, img_rows, img_cols))
	# A 2D Conv layer with 8 filters and relu activation
    x = Conv2D(8, 3, 3,  activation='relu')(digit_input)
	# A maxpooling layer with (2,2) size filter and (2,2) stried
    tower_3 = MaxPooling2D((2, 2), strides=(2, 2))(x)
	# A 2D conv layer with 16 filters and relu activation
    tower_4=Conv2D(16, 3, 3, activation='relu')(tower_3)
	# A maxpooling layer with (2,2) size filter and default, i.e. (1,1), stride
    tower_5 = MaxPooling2D((2, 2), strides=(1, 1))(tower_4)
	# Flattering then a Dense layer, and then dropout with rate 0.5
    tower_6 = Flatten()(tower_5)
    tower_7 = Dense(64, activation='relu')(tower_6)
    tower_7a = Dense(64, activation='relu')(tower_7)
    tower_8 = Dropout(0.5)(tower_7a)
	# Two outputs
    main_output = Dense(10, activation='softmax', name='main_output')(tower_8)
    auxiliary_output = Dense(10, activation='softmax', name='auxiliary_output')(tower_8)
    
    model = Model(input=[digit_input], output=[main_output, auxiliary_output])
    model.compile(loss='categorical_crossentropy',optimizer='adam',  metrics=['accuracy'], loss_weights=[0.5, 0.5])
	#==================== Fetch Data and Fit ===================#
    model.fit(X_train, [y_train[0], y_train[1]], nb_epoch=nb_epoch, batch_size=batch_size, verbose=1)
    objective_score = model.evaluate(X_test, [y_test[0], y_test[1]], batch_size=batch_size) # TO BE COMPLETED.
    print('Evaluation on test set:', dict(zip(model.metrics_names, objective_score)))
	
	#Uncomment the following line if you would like to save your trained model
	#model.save('./current_model_conv.h5')
    if K.backend()== 'tensorflow':
        K.clear_session()
コード例 #6
0
ファイル: mlp_v2.py プロジェクト: wesenu/6.036
def main():
	X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir, use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data
	
	#=================== Model ======================#
	# TO DO: Define a model with input the first image and outputs the two digits. The model variable should be called model and should be based on 
	# a multilayer fully connected network. Consult https://keras.io/getting-started/functional-api-guide/ for the relevant API.
	inputs = Input(shape=(1,img_rows, img_cols))
	x = Dense(64, activation='relu')(Flatten()(inputs))
	x = Dense(1000, activation='relu')(x)
コード例 #7
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data

    #=================== Model ======================#
    # TO DO: Define a model with inputs an image of size 42*28 and outputs the two digits. The model variable should be called model and should be
    # based on a Convolutional Neural Network. Consult  https://keras.io/getting-started/functional-api-guide/ for the relevant API.

    # A setting of parameters used by us was the following:
    # A 2D Conv layer with 8 filters and relu activation
    # A maxpooling layer with (2,2) size filter and (2,2) stried
    # A 2D conv layer with 16 filters and relu activation
    # A maxpooling layer with (2,2) size filter and default, i.e. (1,1), stride
    # Flattering then a Dense layer, and then dropout with rate 0.5
    # Two outputs

    inputs = Input(shape=(1, 42, 28))
    conv_layer = Conv2D(nb_filter=8,
                        nb_row=3,
                        nb_col=3,
                        input_shape=(1, X_train.shape[2], X_train.shape[3]),
                        activation='relu')(inputs)
    max_pooler = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv_layer)
    second_conv_layer = Conv2D(nb_filter=16,
                               nb_row=3,
                               nb_col=3,
                               activation='relu')(max_pooler)
    max_pooler2 = MaxPooling2D(pool_size=(2, 2))(second_conv_layer)
    flat = Flatten()(max_pooler2)
    denser = Dense(64)(flat)
    prediction1 = Dense(10, activation='softmax')(denser)
    prediction2 = Dense(10, activation='softmax')(denser)
    model = Model(input=inputs, output=[prediction1, prediction2])

    model.compile(loss='categorical_crossentropy',
                  optimizer=SGD(lr=0.01, momentum=0.95),
                  metrics=['accuracy'],
                  loss_weights=[0.5, 0.5])
    #==================== Fetch Data and Fit ===================#
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)
    objective_score = model.evaluate(X_test, y_test,
                                     batch_size=batch_size)  # TO BE COMPLETED.
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    #model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #8
0
ファイル: model2.py プロジェクト: wesenu/6.036-ml
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)

    # input layer
    inputs = Input(shape=(1, 42, 28), name='image_input')

    # intermediate layers
    conv_layer1 = Conv2D(8, (3, 3),
                         activation='relu',
                         input_shape=(1, X_train.shape[2],
                                      X_train.shape[3]))(inputs)
    maxpooling1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv_layer1)
    conv_layer2 = Conv2D(16, (3, 3),
                         activation='relu',
                         input_shape=(1, X_train.shape[2],
                                      X_train.shape[3]))(maxpooling1)
    maxpooling2 = MaxPooling2D(pool_size=(2, 2), strides=(1, 1))(conv_layer1)
    flatten = Flatten()(maxpooling2)
    hidden1 = Dense(64, activation='relu')(flatten)
    dropout = Dropout(0.5)(hidden1)

    # create 2 outputs!
    prediction1 = Dense(10, activation='softmax',
                        name="first_prediction")(dropout)
    prediction2 = Dense(10, activation='softmax',
                        name="second_prediction")(dropout)

    model = Model(inputs=inputs, outputs=[prediction1, prediction2])
    model.compile(loss='categorical_crossentropy', optimizer=Adagrad(lr=0.01, epsilon=1e-08, decay=0.0),  \
                  metrics=['accuracy'], loss_weights=[0.5, 0.5])
    #==================== Fetch Data and Fit ===================#
    start = time.time()
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)
    end = time.time()
    print("Training time:", end - start)

    objective_score = model.evaluate(X_test,
                                     y_test,
                                     batch_size=batch_size,
                                     verbose=1,
                                     sample_weight=None)
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    #model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #9
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data

    #=================== Model ======================#
    # TO DO: Define a model with inputs an image of size 42*28 and outputs the two digits. The model variable should be called model and should be
    # based on a Convolutional Neural Network. Consult  https://keras.io/getting-started/functional-api-guide/ for the relevant API.
    #y_train = np_utils.to_categorical(y_train, num_classes)
    #y_test = np_utils.to_categorical(y_test, num_classes)
    # A setting of parameters used by us was the following:
    digit_input = Input(shape=(1, img_rows, img_cols))
    # A 2D Conv layer with 8 filters and relu activation
    x = Conv2D(8, 3, 3, activation='relu')(digit_input)
    # A maxpooling layer with (2,2) size filter and (2,2) stried
    tower_3 = MaxPooling2D((2, 2), strides=(2, 2))(x)
    # A 2D conv layer with 16 filters and relu activation
    tower_4 = Conv2D(16, 3, 3, activation='relu')(tower_3)
    # A maxpooling layer with (2,2) size filter and default, i.e. (1,1), stride
    tower_5 = MaxPooling2D((2, 2), strides=(1, 1))(tower_4)
    # Flattering then a Dense layer, and then dropout with rate 0.5
    tower_6 = Flatten()(tower_5)
    tower_7 = Dense(64, activation='relu')(tower_6)
    tower_8 = Dropout(0.5)(tower_7)
    # Two outputs
    main_output = Dense(10, activation='softmax', name='main_output')(tower_8)
    auxiliary_output = Dense(10, activation='softmax',
                             name='auxiliary_output')(tower_8)

    model = Model(input=[digit_input], output=[main_output, auxiliary_output])
    model.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'],
                  loss_weights=[0.5, 0.5])
    #==================== Fetch Data and Fit ===================#
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)
    objective_score = model.evaluate(X_test, [y_test[0], y_test[1]],
                                     batch_size=batch_size)  # TO BE COMPLETED.
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    #model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #10
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data
    print(np.shape(X_train), np.shape(X_test), np.shape(y_train),
          np.shape(y_test))
    print(type(X_train))

    #=================== Model ======================#
    # TO DO: Define a model with input the first image and outputs the two digits. The model variable should be called model and should be based on
    # a multilayer fully connected network. Consult https://keras.io/getting-started/functional-api-guide/ for the relevant API.

    inputs = Input(shape=(1, 42, 28), name='image_input')
    flatten = Flatten()(inputs)
    hidden1 = Dense(64, activation='relu')(flatten)
    hidden2 = Dense(64, activation='relu')(hidden1)

    # create 2 outputs!
    prediction1 = Dense(10, activation='softmax',
                        name="first_prediction")(hidden2)
    prediction2 = Dense(10, activation='softmax',
                        name="second_prediction")(hidden2)

    model = Model(inputs=inputs, outputs=[prediction1, prediction2])
    model.compile(loss='categorical_crossentropy',
                  optimizer=SGD(lr=0.01,
                                momentum=0.9,
                                decay=0.0,
                                nesterov=True),
                  metrics=['accuracy'])
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)

    objective_score = model.evaluate(X_test,
                                     y_test,
                                     batch_size=batch_size,
                                     verbose=1,
                                     sample_weight=None)
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    #model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #11
0
ファイル: conv.py プロジェクト: wesenu/6.036
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data

    #=================== Model ======================#
    # TO DO: Define a model with inputs an image of size 42*28 and outputs the two digits. The model variable should be called model and should be
    # based on a Convolutional Neural Network. Consult  https://keras.io/getting-started/functional-api-guide/ for the relevant API.

    # A setting of parameters used by us was the following:
    # A 2D Conv layer with 8 filters and relu activation
    # A maxpooling layer with (2,2) size filter and (2,2) stried
    # A 2D conv layer with 16 filters and relu activation
    # A maxpooling layer with (2,2) size filter and default, i.e. (1,1), stride
    # Flattering then a Dense layer, and then dropout with rate 0.5
    # Two outputs

    inputs = Input(shape=(1, img_rows, img_cols))
    x = Conv2D(8, (3, 3), padding='same', activation='relu')(inputs)
    x = MaxPooling2D((2, 2), strides=(2, 2), padding='same')(x)
    x = Conv2D(16, (3, 3), padding='same', activation='relu')(x)
    x = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x)
    x = Flatten()(x)
    x = Dense(64, activation='relu')(x)
    x = Dropout(0.5)(x)
    y = (Dense(10, activation='softmax')(x), Dense(10,
                                                   activation='softmax')(x))
    model = Model(inputs=[inputs], outputs=[y[0], y[1]])

    model.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'],
                  loss_weights=[0.5, 0.5])
    #==================== Fetch Data and Fit ===================#
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)
    objective_score = model.evaluate(X_test, y_test, batch_size=64)
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    #model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #12
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir, use_mini_dataset)
	#Investigate the test set, training set, and the labels to get intuition about the representation of the data

	#=================== Model ======================#
	# TO DO: Define a model with inputs an image of size 42*28 and outputs the two digits. The model variable should be called model and should be 
	# based on a Convolutional Neural. Consult  https://keras.io/getting-started/functional-api-guide/ for the relevant API.

	# A setting of parameters used by us was the following:
	# A 2D Conv layer with 8 filters and relu activation
	# A maxpooling layer with (2,2) size filter and (2,2) stried
	# A 2D conv layer with 16 filters and relu activation
	# A maxpooling layer with (2,2) size filter and default, i.e. (1,1), stride
	# Flattering then a Dense layer, and then dropout with rate 0.5
	# Two outputs
	
    print(np.shape(X_train))
    print(np.shape(X_test))
    print(np.shape(y_train))
    print(np.shape(y_test))
    input_layer = Input(shape=(1,42,28))
    conv_layer = Conv2D(8,(3,3),activation = "tanh")(input_layer)
    pool_layer = MaxPooling2D(pool_size=(2,2), strides=(2,2))(conv_layer)
    conv_layer_2 = Conv2D(16,(3,3),activation = "tanh")(pool_layer)
    max_layer_2 = MaxPooling2D((2,2),(1,1))(conv_layer_2)
    flat_layer = Flatten()(max_layer_2)
    dense_layer = Dense(64, activation = "tanh")(flat_layer)
    dropout = Dropout(0.5)(dense_layer)
    dense_layer_2 = Dense(64)(dropout)
    dropout_2 = Dropout(0.5)(dense_layer_2)
    output = Dense(num_classes, activation = "softmax")(dropout_2)
    output_2 = Dense(num_classes, activation='softmax')(dropout_2)
    model= Model(input_layer,[output,output_2])

    model.compile(loss='kullback_leibler_divergence',optimizer='adam',  metrics=['accuracy'], loss_weights=[0.5, 0.5])
	#==================== Fetch Data and Fit ===================#
    model.fit(X_train, [y_train[0], y_train[1]], nb_epoch=nb_epoch, batch_size=batch_size, verbose=1)
    objective_score = model.evaluate(X_test, y_test, batch_size=64)# TO BE COMPLETED.
    print('Evaluation on test set:', dict(zip(model.metrics_names, objective_score)))
	
	#Uncomment the following line if you would like to save your trained model
    model.save('./current_model_conv.h5')
    if K.backend()== 'tensorflow':
       K.clear_session()
コード例 #13
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data

    #y_train = np_utils.to_categorical(y_train, num_classes)
    #y_test = np_utils.to_categorical(y_test, num_classes)
    #=================== Model ======================#
    # TO DO: Define a model with input the first image and outputs the two digits. The model variable should be called model and should be based on
    # a multilayer fully connected network. Consult https://keras.io/getting-started/functional-api-guide/ for the relevant API.

    # We need to rehape the data back into a 1x42x28 image
    #X_train = np.reshape(X_train, (X_train.shape[0], 1, 42, 28))
    #X_test = np.reshape(X_test, (X_test.shape[0], 1, 42, 28))

    digit_input = Input(shape=(1, img_rows, img_cols))
    x = Flatten()(digit_input)
    x = Dense(64, activation='relu')(x)
    main_output = Dense(10, activation='softmax', name='main_output')(x)
    auxiliary_output = Dense(10, activation='softmax',
                             name='auxiliary_output')(x)

    model = Model(input=digit_input, output=[main_output, auxiliary_output])

    model.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'],
                  loss_weights=[0.5, 0.5])
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)
    objective_score = model.evaluate(X_test, [y_test[0], y_test[1]],
                                     batch_size=batch_size)  #TO  BE COMPLETED.
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    #model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #14
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data

    print(np.shape(X_train))
    print(np.shape(X_test))
    print(np.shape(y_train))
    print(np.shape(y_test))
    input_layer = Input(shape=(1, 42, 28))
    conv_layer = Conv2D(8, (3, 3), activation="tanh")(input_layer)
    pool_layer = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv_layer)
    conv_layer_2 = Conv2D(16, (3, 3), activation="tanh")(pool_layer)
    max_layer_2 = MaxPooling2D((2, 2), (1, 1))(conv_layer_2)
    flat_layer = Flatten()(max_layer_2)
    dense_layer = Dense(64, activation="tanh")(flat_layer)
    dropout = Dropout(0.5)(dense_layer)
    dense_layer_2 = Dense(64)(dropout)
    dropout_2 = Dropout(0.5)(dense_layer_2)
    output = Dense(num_classes, activation="softmax")(dropout_2)
    output_2 = Dense(num_classes, activation='softmax')(dropout_2)
    model = Model(input_layer, [output, output_2])

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'],
                  loss_weights=[0.5, 0.5])
    #==================== Fetch Data and Fit ===================#
    model.fit(X_train, [y_train[0], y_train[1]],
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              verbose=1)
    objective_score = model.evaluate(X_test, y_test,
                                     batch_size=64)  # TO BE COMPLETED.
    print('Evaluation on test set:',
          dict(zip(model.metrics_names, objective_score)))

    #Uncomment the following line if you would like to save your trained model
    model.save('./current_model_conv.h5')
    if K.backend() == 'tensorflow':
        K.clear_session()
コード例 #15
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)

    # Split into train and dev
    dev_split_index = int(9 * len(X_train) / 10)
    X_dev = X_train[dev_split_index:]
    y_dev = [y_train[0][dev_split_index:], y_train[1][dev_split_index:]]
    X_train = X_train[:dev_split_index]
    y_train = [y_train[0][:dev_split_index], y_train[1][:dev_split_index]]

    permutation = np.array([i for i in range(len(X_train))])
    np.random.shuffle(permutation)
    X_train = [X_train[i] for i in permutation]
    y_train = [[y_train[0][i] for i in permutation],
               [y_train[1][i] for i in permutation]]

    # Split dataset into batches
    train_batches = batchify_data(X_train, y_train, batch_size)
    dev_batches = batchify_data(X_dev, y_dev, batch_size)
    test_batches = batchify_data(X_test, y_test, batch_size)
    # print(train_batches[0]['x'].shape, train_batches[0]['y'].shape)
    # batch[i]['x'] is (64, 1, 42, 28) = (batch_size, 1, img_rows, img_cols)
    # batch[i]['y'] is (2, 64) = (num_labels, batch_size)

    # print(len(X_train), len(y_train))  # 36000, 2
    # print(len(X_dev), len(y_dev))  # 4000, 2
    # print(len(train_batches), len(dev_batches), len(test_batches))  # 562, 62, 62

    # Load model
    input_dimension = img_rows * img_cols
    model = MLP(input_dimension)

    # Train
    train_model(train_batches, dev_batches, model)

    ## Evaluate the model on test data
    loss, acc = run_epoch(test_batches, model.eval(), None)
    print(
        'Test loss1: {:.6f}  accuracy1: {:.6f}  loss2: {:.6f}   accuracy2: {:.6f}'
        .format(loss[0], acc[0], loss[1], acc[1]))
コード例 #16
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir, use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data

    #=================== Model ======================#
    # TO DO: Define a model with inputs an image of size 42*28 and outputs the two digits. The model variable should be called model and should be 
    # based on a Convolutional Neural Network. Consult  https://keras.io/getting-started/functional-api-guide/ for the relevant API.

    # input layer
    inputs = Input(shape=(1, 42, 28), name='image_input')

    # intermediate layers
    conv_layer1 = Conv2D(8, (3,3), activation='relu', input_shape=(1, X_train.shape[2], X_train.shape[3]))(inputs)
    maxpooling1 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(conv_layer1)
    conv_layer2 = Conv2D(16, (3,3), activation='relu', input_shape=(1, X_train.shape[2], X_train.shape[3]))(maxpooling1)
    maxpooling2 = MaxPooling2D(pool_size=(2,2), strides=(1,1))(conv_layer1)
    flatten = Flatten()(maxpooling2)
    hidden1 = Dense(64, activation='relu')(flatten)
    hidden2 = Dense(64, activation='relu')(hidden1)
    dropout = Dropout(0.5)(hidden2)

    # create 2 outputs!
    prediction1 = Dense(10, activation='softmax', name="first_prediction")(dropout)
    prediction2 = Dense(10, activation='softmax', name="second_prediction")(dropout)

    model = Model(inputs=inputs, outputs=[prediction1, prediction2])
    model.compile(loss='categorical_crossentropy',optimizer='sgd',  metrics=['accuracy'], loss_weights=[0.5, 0.5])
    #==================== Fetch Data and Fit ===================#
    start = time.time()
    model.fit(X_train, [y_train[0], y_train[1]], nb_epoch=nb_epoch, batch_size=batch_size, verbose=1)
    end = time.time()
    print("Training time:", end-start)

    objective_score = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=1, sample_weight=None)
    print('Evaluation on test set:', dict(zip(model.metrics_names, objective_score)))
    
    #Uncomment the following line if you would like to save your trained model
    #model.save('./current_model_conv.h5')
    if K.backend()== 'tensorflow':
        K.clear_session()
コード例 #17
0
def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)

    # Split into train and dev
    dev_split_index = int(9 * len(X_train) / 10)
    X_dev = X_train[dev_split_index:]
    y_dev = [y_train[0][dev_split_index:], y_train[1][dev_split_index:]]
    X_train = X_train[:dev_split_index]
    y_train = [y_train[0][:dev_split_index], y_train[1][:dev_split_index]]

    permutation = torch.randperm(len(X_train))
    X_train = X_train[permutation]
    y_train = [y_train[0][permutation], y_train[1][permutation]]

    # Split dataset into batches
    train_batches = batchify_data(X_train, y_train, batch_size)
    dev_batches = batchify_data(X_dev, y_dev, batch_size)
    test_batches = batchify_data(X_test, y_test, batch_size)

    # Load model
    input_dimension = img_rows * img_cols
    model = CNN()

    # Move model to the GPU
    if torch.cuda.is_available():
        model = model.to(device)
        print("----------------- Using the Device: GPU -----------------")
    else:
        print("----------------- Using the Device: CPU -----------------")

    # Train
    train_model(train_batches, dev_batches, model)

    # Evaluate the model on test data
    loss, acc = run_epoch(test_batches, model.eval(), None)
    print(
        'Test loss1: {:.6f}  accuracy1: {:.6f}  loss2: {:.6f}   accuracy2: {:.6f}'
        .format(loss[0], acc[0], loss[1], acc[1]))
コード例 #18
0
def prep_data(path_to_data_dir, use_mini_dataset):
    '''
    
    Load the dataset
    '''

    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)

    # Split into train and dev
    dev_split_index = int(9 * len(X_train) / 10)
    X_dev = X_train[dev_split_index:]
    y_dev = [y_train[0][dev_split_index:], y_train[1][dev_split_index:]]
    X_train = X_train[:dev_split_index]
    y_train = [y_train[0][:dev_split_index], y_train[1][:dev_split_index]]

    permutation = np.array([i for i in range(len(X_train))])
    np.random.shuffle(permutation)
    X_train = [X_train[i] for i in permutation]
    y_train = [[y_train[0][i] for i in permutation],
               [y_train[1][i] for i in permutation]]

    return X_train, y_train, X_dev, y_dev, X_test, y_test
コード例 #19
0
ファイル: mlp_v3.py プロジェクト: wesenu/6.036
def main():
	X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir, use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data
	
	#=================== Model ======================#
	# TO DO: Define a model with input the first image and outputs the two digits. The model variable should be called model and should be based on 
	# a multilayer fully connected network. Consult https://keras.io/getting-started/functional-api-guide/ for the relevant API.
	inputs = Input(shape=(1,img_rows, img_cols))
	#x = Dense(64, activation='relu')((inputs))
	x=Conv2D(8,(3,3), padding='same', activation = 'relu')(inputs)
	x=MaxPooling2D((2,2), strides=(1,1), padding='same')(x)
	x=Flatten()(x)
	x = Dense(64, activation='relu')(x)
	y = [Dense(10, activation='softmax')(x), Dense(10, activation='softmax')(x)]
	model = Model(inputs=[inputs], outputs=[y[0],y[1]])
	model.compile(loss='categorical_crossentropy',optimizer='sgd',  metrics=['accuracy'], loss_weights=[0.5, 0.5])
	model.fit(X_train, [y_train[0], y_train[1]], nb_epoch=nb_epoch, batch_size=batch_size, verbose=1)
	objective_score = model.evaluate(X_test, y_test, batch_size=32) #TO  BE COMPLETED
	print('Evaluation on test set:', dict(zip(model.metrics_names, objective_score)))
	
	#Uncomment the following line if you would like to save your trained model
	#model.save('./current_model_conv.h5')
	if K.backend()== 'tensorflow':
		K.clear_session()
コード例 #20
0
ファイル: mlp.py プロジェクト: MTSavran/DeepLearning
from keras.models import Sequential, Model
from keras.layers import Input
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras import backend as K

path_to_data_dir = '../Datasets/'
use_mini_dataset = True
K.set_image_dim_ordering('th')

batch_size = 64
nb_classes = 10
nb_epoch = 30
num_classes = 10
img_rows, img_cols = 42, 28
# input image dimensions
X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                              use_mini_dataset)

#print ("X_train first element", )


def main():
    X_train, y_train, X_test, y_test = U.get_data(path_to_data_dir,
                                                  use_mini_dataset)
    #Investigate the test set, training set, and the labels to get intuition about the representation of the data

    #=================== Model ======================#
    # TO DO: Define a model with input the first image and outputs the two digits. The model variable should be called model and should be based on
    # a multilayer fully connected network. Consult https://keras.io/getting-started/functional-api-guide/ for the relevant API.

    # model = Model(input=digit_input, output=[prediction1, prediction2])