def train_model(feature_layers, classification_layers, image_list, nb_epoch, nb_classes, img_rows, img_cols, weights=None): 
    # Create testset data for cross-val
    num_images = len(image_list)
    test_size = int(0.2 * num_images)
    print("Train size: ", num_images-test_size)
    print("Test size: ", test_size)

    model = Sequential()
    for l in feature_layers + classification_layers:
        model.add(l)

    if not(weights is None):
        model.set_weights(weights)

    # let's train the model using SGD + momentum (how original).
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)
    
    print('Using real time data augmentation')
    for e in range(nb_epoch):
        print('-'*40)
        print('Epoch', e)
        print('-'*40)
        print('Training...')
        # batch train with realtime data augmentation
        progbar = generic_utils.Progbar(num_images-test_size)
        for X_batch, Y_batch in flow(image_list[0:-test_size]):
            X_batch = X_batch.reshape(X_batch.shape[0], 3, img_rows, img_cols)
            Y_batch = np_utils.to_categorical(Y_batch, nb_classes)
            loss = model.train_on_batch(X_batch, Y_batch)
            progbar.add(X_batch.shape[0], values=[('train loss', loss)])

        print('Testing...')
        # test time!
        progbar = generic_utils.Progbar(test_size)
        for X_batch, Y_batch in flow(image_list[-test_size:]):
            X_batch = X_batch.reshape(X_batch.shape[0], 3, img_rows, img_cols)
            Y_batch = np_utils.to_categorical(Y_batch, nb_classes)
            score = model.test_on_batch(X_batch, Y_batch)
            progbar.add(X_batch.shape[0], values=[('test loss', score)])
    return model, model.get_weights()
class LSTM_RNN:

    def __init__(self, look_back, dropout_probability = 0.2, init ='he_uniform', loss='mse', optimizer='rmsprop'):
        self.rnn = Sequential()
        self.look_back = look_back
        self.rnn.add(LSTM(10, stateful = True, batch_input_shape=(1, 1, 1), init=init))
        self.rnn.add(Dropout(dropout_probability))
        self.rnn.add(Dense(1, init=init))
        self.rnn.compile(loss=loss, optimizer=optimizer)

    def batch_train_test(self, trainX, trainY, testX, testY, nb_epoch=150):
        print('Training LSTM-RNN...')
        for epoch in range(nb_epoch):
            print('Epoch '+ str(epoch+1) +'/{}'.format(nb_epoch))
            training_losses = []
            testing_losses = []
            for i in range(len(trainX)):
                y_actual = trainY[i]
                for j in range(self.look_back):
                    training_loss = self.rnn.train_on_batch(np.expand_dims(np.expand_dims(trainX[i][j], axis=1), axis=1),
                                                       np.array([y_actual]))
                    training_losses.append(training_loss)
                self.rnn.reset_states()

            print('Mean training loss = {}'.format(np.mean(training_losses)))

            mean_testing_loss = []
            for i in range(len(testX)):
                for j in range(self.look_back):
                    testing_loss = self.rnn.test_on_batch(np.expand_dims(np.expand_dims(testX[i][j], axis=1), axis=1),
                                                          np.array([testY[i]]))
                    testing_losses.append(testing_loss)
                self.rnn.reset_states()

                for j in range(self.look_back):
                    y_pred = self.rnn.predict_on_batch(np.expand_dims(np.expand_dims(testX[i][j], axis=1), axis=1))
                self.rnn.reset_states()

            mean_testing_loss = np.mean(testing_losses)
            print('Mean testing loss = {}'.format(mean_testing_loss))
        return mean_testing_loss
    print('Iteration', iteration)

    print("Training")
    progbar = generic_utils.Progbar(train_num_samples)
    gen = samples_generator(train_sequences, batch_size,
                            num_samples=train_num_samples)
    for X, y in gen:
        loss, accuracy = model.train_on_batch(X, y, accuracy=True)
        progbar.add(batch_size, values=[("train loss", loss),
                    ("train acc", accuracy)])
    print()

    print("Validating")
    progbar = generic_utils.Progbar(valid_num_samples)
    gen = samples_generator(valid_sequences, batch_size,
                            num_samples=valid_num_samples)
    valid_loss = 0
    for X, y in gen:
        loss, accuracy = model.test_on_batch(X, y, accuracy=True)
        progbar.add(batch_size, values=[("valid loss", loss),
                    ("valid acc", accuracy)])
        valid_loss += loss
    print()
    valid_loss /= float(valid_num_samples)

    print("Valid Loss: {}, Best Loss: {}".format(valid_loss, best_loss))
    if valid_loss < best_loss:
        print("Saving model")
        save_model(model, "sentence_model")
        best_loss = valid_loss
def run():
    datadir = "/reg/d/ana01/temp/davidsch/ImgMLearnFull"
    h5files = glob(os.path.join(datadir, "amo86815_mlearn-r070*.h5"))
    h5files.extend(glob(os.path.join(datadir, "amo86815_mlearn-r071*.h5")))
#    h5files = ["/reg/d/ana01/temp/davidsch/ImgMLearnFull/amo86815_mlearn-r071-c0000.h5",
#               "/reg/d/ana01/temp/davidsch/ImgMLearnFull/amo86815_mlearn-r071-c0001.h5",
#               "/reg/d/ana01/temp/davidsch/ImgMLearnFull/amo86815_mlearn-r071-c0002.h5"]
    assert len(h5files)>0
    
    datareader = H5MiniBatchReader(h5files=h5files,
                                   minibatch_size=32,
                                   validation_size=64,
                                   feature_dataset='xtcavimg',
                                   label_dataset='acq.peaksLabel',
                                   return_as_one_hot=True,
                                   feature_preprocess=['log','mean'],
                                   number_of_batches=None,
                                   class_labels_max_imbalance_ratio=1.0,
                                   add_channel_to_2D='channel_row_column',
                                   max_mb_to_preload_all=None,
                                   random_seed=None,
                                   verbose=True)  

    validation_features, validation_labels = datareader.get_validation_set()

    print("starting to build and compile keras/theano model...")
    sys.stdout.flush()
    t0 = time.time()
    model = Sequential()

    ## layer 1
    kern01_W_init = (0.06/2.0)*scipy.stats.truncnorm.rvs(-2.0, 2.0, size=(8,1,8,8)).astype(np.float32)
    kern01_B_init = np.zeros(8,dtype=np.float32)
    model.add(Convolution2D(8,8,8, border_mode='same', weights=[kern01_W_init, kern01_B_init],
                            input_shape=datareader.features_placeholder_shape()[1:]))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(10,10), strides=(13,13)))
    
    ## layer 2
    kern02_W_init = (0.06/2.0)*scipy.stats.truncnorm.rvs(-2.0, 2.0, size=(8,8,6,6)).astype(np.float32)
    kern02_B_init = np.zeros(8,dtype=np.float32)
    model.add(Convolution2D(8,6,6, border_mode='same', weights=[kern02_W_init, kern02_B_init]))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(10,10), strides=(13,13)))
    
    model.add(Flatten())
    
    ## layer 3
    H03_W_init =  (0.06/2.0)*scipy.stats.truncnorm.rvs(-2.0, 2.0, size=(96,16)).astype(np.float32)
    H03_B_init = np.zeros(16,dtype=np.float32)
    model.add(Dense(16, weights=[H03_W_init, H03_B_init]))
    model.add(Activation('relu'))
    
    ## layer 4
    H04_W_init =  (0.06/2.0)*scipy.stats.truncnorm.rvs(-2.0, 2.0, size=(16, datareader.num_outputs())).astype(np.float32)
    H04_B_init = np.zeros(datareader.num_outputs(),dtype=np.float32)
    model.add(Dense(datareader.num_outputs(), weights=[H04_W_init, H04_B_init]))
    model.add(Activation('softmax'))

    sgd = SGD(lr=0.01, decay=0.0004, momentum=0.96)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)
    print("building/compiling theano model took %.2f sec" % (time.time()-t0),)
    sys.stdout.flush()

    for step_number in range(3000):
        t0 = time.time()
        train_features, train_labels = datareader.get_next_minibatch()
        model.train_on_batch(train_features, train_labels)
        print("step %3d took %.2f sec." % (step_number, time.time()-t0))
        sys.stdout.flush()

    print("Starting evaluation.")
    t0 = time.time()
    loss, validation_accuracy = model.test_on_batch(validation_features, validation_labels, accuracy=True, sample_weight=None)
    print("validation accuracy: %.2f%%" % (100.0*validation_accuracy,))
    print("evaluation took %.2f sec" % (time.time()-t0,))
Example #5
0
model.compile(loss=qri.mae_clip, optimizer=sgd)

# Use early stopping and saving as callbacks
early_stop = EarlyStopping(monitor='val_loss', patience=10)
save_best = ModelCheckpoint("models/%s.mdl" % MDL_NAME, save_best_only=True)
callbacks = [early_stop, save_best]

# Train model
t0 = time.time()
hist = model.fit(train_set[0], train_set[1], validation_data=valid_set,
                 verbose=2, callbacks=callbacks, nb_epoch=1000, batch_size=20)
time_elapsed = time.time() - t0

# Load best model
model.load_weights("models/%s.mdl" % MDL_NAME)

# Print time elapsed and loss on testing dataset
test_set_loss = model.test_on_batch(test_set[0], test_set[1])
print "\nTime elapsed: %f s" % time_elapsed
print "Testing set loss: %f" % test_set_loss

# Save results
qri.save_results("results/%s.out" % MDL_NAME, time_elapsed, test_set_loss)
qri.save_history("models/%s.hist" % MDL_NAME, hist.history)

# Plot training and validation loss
qri.plot_train_valid_loss(hist.history)

# Make predictions
qri.plot_test_predictions(model, train_set)
Example #6
0
        inputs /= 255
        loss, acc = model.train_on_batch(inputs, targets)
        train_loss += loss
        train_acc += acc
        train_batches += 1

    # And a full pass over the validation data:
    val_loss = 0
    val_acc = 0
    val_batches = 0
    test_str.reset() # re-start streaming
    for inputs, targets, pad in val_str:
        if pad: # not full batch
            break
        inputs /= 255
        loss, acc = model.test_on_batch(inputs, targets)
        val_loss += loss
        val_acc += acc
        val_batches += 1

    # Then we print the results for this epoch:
    print("Epoch {}/{} time {:.3f}s; train loss: {:.6f} acc:{:.6f}; val loss: {:.6f} acc:{:.2f}".format(
        epoch + 1, nb_epoch, time.time() - start_time,
        train_loss/train_batches, train_acc/train_batches,
        val_loss/val_batches, val_acc/val_batches))

# After training, we compute and print the test error:
test_loss = 0
test_acc = 0
test_batches = 0
for inputs, targets, pad in test_str:
Example #7
0
class rnnmlp ():
        def __init__(self, r=(21, 109), dt=0.3):
                self.r=r
                self.dt=dt
                self.rnnModel = Sequential()
                self.maxFeatures=r[1]-r[0] +1

	'''
	simple RNN model, 
	'''
        def SimpleRNNModel(self, nHidden=120, lr = 0.01):
                self.rnnModel.add(SimpleRNN( nHidden, input_shape =( None, self.maxFeatures), activation='sigmoid', return_sequences=True))
                self.rnnModel.add(TimeDistributedDense(self.maxFeatures))
                self.rnnModel.add(Activation('softmax'))
                rmsprop = RMSprop(lr=lr, rho=0.9, epsilon=1e-06)
                self.rnnModel.compile(loss='categorical_crossentropy', optimizer=rmsprop)

	'''
	LSTM model
	'''
        def LSTMModel(self, nHidden=150, lr = 0.01):
#               print('nHidden: %i\tlr: %.3f' % ( nHidden, lr) )
                self.rnnModel.add(GRU( nHidden, activation='sigmoid', input_shape =( None, self.maxFeatures), return_sequences=True))
#                self.rnnModel.add(LSTM( nHidden, activation='sigmoid', input_shape =( None, nHidden), return_sequences=True))
                self.rnnModel.add(TimeDistributedDense(nHidden))
                self.rnnModel.add(Activation('relu'))
                self.rnnModel.add(TimeDistributedDense(self.maxFeatures))
                self.rnnModel.add(Activation('softmax'))
                rmsprop = RMSprop(lr=lr, rho=0.9, epsilon=1e-06)
                self.rnnModel.compile(loss='categorical_crossentropy', optimizer=rmsprop)

        '''
	train module :
	train model , 
	file_name, the name of train or test file
	weight_save_file, save the model parameters
	'''
	def train(self, file_name, weight_save_file, batch_size=1, num_epoch=200):
                print('load data ---------------')

                file_train=os.path.join(os.path.split(os.path.dirname(__file__))[0],
                                'data',file_name,'train','*.mid')
                dataset = [midiread(f, self.r, self.dt).piano_roll.astype(theano.config.floatX) for f in glob.glob(file_train)]

                file_test=os.path.join(os.path.split(os.path.dirname(__file__))[0],
                                'data',file_name,'test','*.mid')
                testdataset = [midiread(f, self.r, self.dt).piano_roll.astype(theano.config.floatX) for f in glob.glob(file_test)]
                print('load done --------------')
                try:
                        for epoch in range(num_epoch):
                                t0 = time.time()
                                numpy.random.shuffle(dataset)
                                costs = []
                                accuracys = []
                                for s, sequence in enumerate(dataset):
                                        y = numpy.hstack((sequence,numpy.zeros((sequence.shape[0],1)) ))
                                        x = numpy.roll(y, 1, axis=0)
                                        x[0,:]=0
                                        x[0,self.maxFeatures-1]=1
                                        cost, accuracy= self.rnnModel.train_on_batch(numpy.array([x]), numpy.array([y]), accuracy=True)
                                        costs.append(cost)
                                        accuracys.append(accuracy)

                                print('epoch: %i/%i\tcost: %.5f\taccu: %.5f\ttime: %.4f s' % (epoch+1, num_epoch, numpy.mean(costs), numpy.mean(accuracys),time.time()-t0))
                                sys.stdout.flush()
                                test_accu=self.evaluate(testdataset)
                                print('test_accu: %.5f' % ( numpy.mean(test_accu)) )
                        self.rnnModel.save_weights(weight_save_file)
                except KeyboardInterrupt:
                        print('interrupt by user !')
	
	'''
	evaluate module :
	evaluate model with test data, compute cost and accuracy
	'''
        def evaluate(self, test_dataset):
                test_accuracy =[]
                for s, sequence in enumerate(test_dataset):
                        test_y = numpy.hstack((sequence,numpy.zeros((sequence.shape[0],1)) ))
                        test_x = numpy.roll(test_y, 1, axis=0)
                        test_x[0,:]=0
                        test_x[0,self.maxFeatures-1]=1
                        cost, accu = self.rnnModel.test_on_batch(numpy.array([test_x]),numpy.array([test_y]), accuracy=True)
                        test_accuracy.append(accu)
                return test_accuracy

	'''
	generate function : 
	generate music or chord,
	init_chord: the first note of the generate sequence
	file_name: file to save the sequence of generate notes
	LS : if true , add Lsystem , generate chord
		if false, no Lsystem, generate music notes
	chord_name: chord name under condition of LS = True
	chord_file: notes of all kinds of chords, load file
	state_file: Lsystem model parameters, load file
	n_steps: the length of generate sequence
	r: notes which counts
	'''
        def generate(self, init_chord, file_name, LS=False, chord_name=None, chord_file=None, state_file=None, n_steps=80, r=(21,109)):
		if(LS):
			Lsystem = LSystem(chord_name, init_chord, chord_file, state_file, r)
                init_sequence = numpy.zeros((1, n_steps +1, self.maxFeatures))
                init_sequence[:, 0, init_chord-self.r[0]] = 1
                for i in numpy.arange(n_steps):
                        probs = self.rnnModel.predict_proba(init_sequence)[:, i, :]
                        for j in numpy.arange(len(init_sequence)):
				if(LS):
					ind = Lsystem.getMaxProbs(probs[j,0:(self.maxFeatures-1)],True)
				else:
					ind = maxProbs(probs[j,0:(self.maxFeatures-1)])
                                init_sequence[j, i+1, ind] = 1

                generate_sq = [sq[:,0:(self.maxFeatures-1)].nonzero()[1] for sq in init_sequence]
                print(generate_sq[0] + self.r[0])
		if(LS):
			print(Lsystem.cur_chord)
			print(Lsystem.cur_state)
			print(Lsystem.cur_opes)
                midiwrite(file_name, init_sequence[0,:,0:(self.maxFeatures-1)], self.r, self.dt)
		extent = (0, self.dt * len(init_sequence[0,:,0:(self.maxFeatures-1)])) + self.r
		pylab.figure()
		pylab.imshow(init_sequence[0,:,0:(self.maxFeatures-1)].T, origin='lower', aspect='auto',interpolation='nearest', cmap=pylab.cm.gray_r,extent=extent)
		pylab.xlabel('time (s)')
		pylab.ylabel('MIDI note number')
		pylab.title('generated piano-roll')



	'''
	load module: load model
	'''
        def loadModel(self, weight_save_file):
                self.rnnModel.load_weights(weight_save_file)
Example #8
0
        horizontal_flip=False,  # randomly flip images
        vertical_flip=False)  # randomly flip images

    # compute quantities required for featurewise normalization
    # (std, mean, and principal components if ZCA whitening is applied)
    datagen.fit(X_train)

    for e in range(nb_epoch):
        print('-'*40)
        print('Epoch', e)
        print('-'*40)
        print("Training...")

        progbar = generic_utils.Progbar(X_train.shape[0])
        for X_batch, Y_batch in datagen.flow(X_train, Y_train):
            score, acc = model.test_on_batch(X_batch, Y_batch, accuracy=True)
            progbar.add(X_batch.shape[0], values=[("train accuracy", acc)])

        print("Testing...")

        progbar = generic_utils.Progbar(X_test.shape[0])
        for X_batch, Y_batch in datagen.flow(X_test, Y_test):
            score, acc = model.test_on_batch(X_batch, Y_batch, accuracy=True)
            progbar.add(X_batch.shape[0], values=[("test accuracy", acc)])

        # test time!
        for X_batch, Y_batch in datagen.flow(X_te_orig, np.ones((1,X_te_orig.shape[0])), batch_size = X_te_orig.shape[0]):
            y_te = model.predict_classes(X_batch)

        save_out(y_te,labels_string,sorted_files_te,submission_fname)
Example #9
0
                X, y = get_data(files[n:n+batch_size], n)
            else:
                X, y = get_data(files[n:], n)

            X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
            X_train = np.array(X_train)
            X_test = np.array(X_test)
            y_train = np.array(y_train)
            y_test = np.array(y_test)

            # convert class vectors to binary class matrices
            Y_train = np_utils.to_categorical(y_train, nb_classes)
            Y_test = np_utils.to_categorical(y_test, nb_classes)

            model.train_on_batch(X_train, Y_train)
            l, a = model.test_on_batch(X_test, Y_test, accuracy=True)

            acc.append(a)
            loss.append(l)
        print ''
        print "Val_loss", (sum(loss) / len(loss))
        print "Val_acc", (sum(acc) / len(acc))

    # with random batch draws....
    # -------
    # Epoch 1
    # Val_loss 0.889370705837
    # Val_acc 0.479363625794

    # -------
    # Epoch 2
        print('-'*40)
        print('Epoch', e)
        print('-'*40)
        print('Training...')
        # batch train with realtime data augmentation
        progbar = generic_utils.Progbar(X_train.shape[0])
        for X_batch, Y_batch in datagen.flow(X_train, Y_train,batch_size=128):
            loss = modelCascade.train_on_batch(X_batch, Y_batch,accuracy=True)
            progbar.add(X_batch.shape[0], values=[('train loss', loss[0]),('train accuracy',loss[1])])      
        print('Testing...')
    # test time!
        accuracyArray = list()
        lossArray = list()
        progbar = generic_utils.Progbar(X_test.shape[0])
        for X_batch, Y_batch in datagen.flow(X_test, Y_test):
            score = modelCascade.test_on_batch(X_batch, Y_batch,accuracy=True)
            lossArray.append(score[0])
            accuracyArray.append(score[1])
            progbar.add(X_batch.shape[0], values=[('test loss', score[0]),('test accuracy',score[1])])
        lossIteration1.append(np.mean(lossArray))
        accuracyIteration1.append(np.mean(accuracyArray))            

    weightsLayer1 = modelCascade.layers[0].get_weights()

    print('SECOND ITERATION STARTING')
    #####SECOND ITERATION
    #MODEL THAT IS USED TO GENERATE INPUT HAT
    modelIH = Sequential()
    modelIH.add(Convolution2D(32, 3, 3, border_mode='same',
                        input_shape=(img_channels, img_rows, img_cols),weights=weightsLayer1))
    modelIH.add(Activation('relu'))
Example #11
0
def main():
    # the data, shuffled and split between tran and test sets
    data, labels = load_data(filename)
    mat = scipy.io.loadmat(subjectsFilename, mat_dtype=True)
    subjNumbers = np.squeeze(mat['subjectNum'])     # subject IDs for each trial

    # Creating the folds
    # kf = StratifiedKFold(np.squeeze(labels), n_folds=ksplit, shuffle=True, random_state=123)
    # kf = KFold(labels.shape[0], n_folds=ksplit, shuffle=True, random_state=123)
    # fold_pairs = [(tr, ts) for (tr, ts) in kf]

    # Leave-Subject-Out cross validation
    fold_pairs = []
    for i in np.unique(subjNumbers):
        ts = subjNumbers == i
        tr = np.squeeze(np.nonzero(np.bitwise_not(ts)))
        ts = np.squeeze(np.nonzero(ts))
        np.random.shuffle(tr)       # Shuffle indices
        np.random.shuffle(ts)
        fold_pairs.append((tr, ts))


    validScores, testScores = [], []
    for foldNum, fold in enumerate(fold_pairs):
        (X_train, y_train), (X_valid, y_valid), (X_test, y_test) = reformatInput(data, labels, fold)
        print('X_train shape:', X_train.shape)
        print(X_train.shape[0], 'train samples')
        print(X_valid.shape[0], 'valid samples')
        print(X_test.shape[0], 'test samples')

        X_train = X_train.astype("float32", casting='unsafe')
        X_valid = X_valid.astype("float32", casting='unsafe')
        X_test = X_test.astype("float32", casting='unsafe')
        # convert class vectors to binary class matrices
        Y_train = np_utils.to_categorical(y_train, nb_classes)
        Y_valid = np_utils.to_categorical(y_valid, nb_classes)
        Y_test = np_utils.to_categorical(y_test, nb_classes)

        # Building the network
        model = Sequential()

        model.add(Convolution2D(40, 3, 3, border_mode='full',
                                input_shape=(image_dimensions, shapex, shapey)))
        model.add(Activation('relu'))
        model.add(Convolution2D(40, 3, 3))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))

        # model.add(Convolution2D(80, 3, 3, border_mode='full'))
        # model.add(Activation('relu'))
        # model.add(Convolution2D(80, 3, 3))
        # model.add(Activation('relu'))
        # model.add(MaxPooling2D(pool_size=(2, 2)))
        # model.add(Dropout(0.25))

        model.add(Flatten())
        model.add(Dense(1024))
        model.add(Activation('relu'))
        model.add(Dropout(0.5))
        model.add(Dense(nb_classes))
        model.add(Activation('softmax'))

        # model.add(Convolution2D(nb_filters[0], image_dimensions, nb_conv[0], nb_conv[0], border_mode='full'))
        # # model.add(BatchNormalization([nb_filters[0], nb_conv[0], nb_conv[0], image_dimensions]))
        # model.add(Activation('relu'))
        # model.add(Convolution2D(nb_filters[0], nb_filters[0], nb_conv[0], nb_conv[0]))
        # model.add(Activation('relu'))
        # model.add(MaxPooling2D(poolsize=(nb_pool[0], nb_pool[0])))
        # model.add(Dropout(0.25))
        #
        # model.add(Convolution2D(nb_filters[1], nb_filters[0], nb_conv[0], nb_conv[0], border_mode='full'))
        # model.add(Activation('relu'))
        # model.add(Convolution2D(nb_filters[1], nb_filters[1], nb_conv[1], nb_conv[1]))
        # model.add(Activation('relu'))
        # model.add(MaxPooling2D(poolsize=(nb_pool[1], nb_pool[1])))
        # model.add(Dropout(0.25))
        #
        # model.add(Flatten())
        # # the image dimensions are the original dimensions divided by any pooling
        # # each pixel has a number of filters, determined by the last Convolution2D layer
        # model.add(Dense(nb_filters[-1] * (shapex / nb_pool[0] / nb_pool[1]) * (shapey / nb_pool[0] / nb_pool[1]), 1024))
        # # model.add(BatchNormalization([1024]))
        # model.add(Activation('relu'))
        # model.add(Dropout(0.5))
        # model.add(Dense(1024, nb_classes))
        # model.add(Activation('softmax'))

        # let's train the model using SGD + momentum (how original).
        sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
        model.compile(loss='categorical_crossentropy', optimizer=sgd)

        if not data_augmentation:
            print("Not using data augmentation or normalization")

            X_train = X_train.astype("float32", casting='unsafe')
            X_test = X_test.astype("float32", casting='unsafe')
            X_train /= 255.
            X_test /= 255.
            model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True)
            score, accu = model.evaluate(X_test, Y_test, batch_size=batch_size, show_accuracy=True)
            print('Test accuracy:', accu)

        else:
            print("Using real time data augmentation")
            # X_train = (X_train - np.mean(X_train, axis=0)) / np.std(X_train.flatten(), axis=0)
            # X_valid = (X_valid - np.mean(X_valid, axis=0)) / np.std(X_valid.flatten(), axis=0)
            # X_test = (X_test - np.mean(X_test, axis=0)) / np.std(X_test.flatten(), axis=0)
            # X_train = (X_train - np.mean(X_train, axis=0))
            # X_valid = (X_valid - np.mean(X_train, axis=0))
            # X_test = (X_test - np.mean(X_train, axis=0))
            # this will do preprocessing and realtime data augmentation
            datagen = ImageDataGenerator(
                featurewise_center=True,  # set input mean to 0 over the dataset
                samplewise_center=False,  # set each sample mean to 0
                featurewise_std_normalization=False,  # divide inputs by std of the dataset
                samplewise_std_normalization=False,  # divide each input by its std
                zca_whitening=False,  # apply ZCA whitening
                rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
                width_shift_range=0,  # randomly shift images horizontally (fraction of total width)
                height_shift_range=0,  # randomly shift images vertically (fraction of total height)
                horizontal_flip=False,  # randomly flip images
                vertical_flip=False)  # randomly flip images

            # compute quantities required for featurewise normalization
            # (std, mean, and principal components if ZCA whitening is applied)
            datagen.fit(X_train)
            best_validation_accu = 0

            for e in range(nb_epoch):

                print('-'*40)
                print('Epoch', e)
                print('-'*40)
                print("Training...")
                # batch train with realtime data augmentation
                progbar = generic_utils.Progbar(X_train.shape[0])
                for X_batch, Y_batch in datagen.flow(X_train, Y_train, batch_size=batch_size):
                    score, trainAccu = model.train_on_batch(X_batch, Y_batch, accuracy=True)
                    progbar.add(X_batch.shape[0], values=[("train accuracy", trainAccu)])

                print("Validating...")
                # Validation time!
                progbar = generic_utils.Progbar(X_valid.shape[0])
                epochValidAccu = []
                for X_batch, Y_batch in datagen.flow(X_valid, Y_valid, batch_size=batch_size):
                    score, validAccu = model.test_on_batch(X_batch, Y_batch, accuracy=True)
                    epochValidAccu.append(validAccu)
                    progbar.add(X_batch.shape[0], values=[("validation accuracy", validAccu)])
                meanValidAccu = np.mean(epochValidAccu)
                if meanValidAccu > best_validation_accu:
                    best_validation_accu = meanValidAccu
                    best_iter = e
                    print("Testing...")
                    # test time!
                    progbar = generic_utils.Progbar(X_test.shape[0])
                    epochTestAccu = []
                    for X_batch, Y_batch in datagen.flow(X_test, Y_test, batch_size=batch_size):
                        score, testAccu = model.test_on_batch(X_batch, Y_batch, accuracy=True)
                        epochTestAccu.append(testAccu)
                        progbar.add(X_batch.shape[0], values=[("test accuracy", testAccu)])
                    model.save_weights('weigths_{0}'.format(foldNum), overwrite=True)
            validScores.append(best_validation_accu)
            testScores.append(np.mean(epochTestAccu))
    scipy.io.savemat('cnn_results', {'validAccu': validScores, 'testAccu': testScores})
    print ('Average valid accuracies: {0}'.format(np.mean(validScores)))
    print ('Average test accuracies: {0}'.format(np.mean(testScores)))
        print('-'*40)
        print('Epoch', e)
        print('-'*40)
        print("Training...")
        # batch train with realtime data augmentation
        progbar = generic_utils.Progbar(X_train.shape[0])
        for X_batch, Y_batch in datagen.flow(X_train, Y_train):
            
            loss = model.train_on_batch(X_batch, X_batch.reshape(X_batch.shape[0],X_train.shape[2]**2*3))
            progbar.add(X_batch.shape[0], values=[("train loss", loss)])

        print("Testing...")
        # test time!
        progbar = generic_utils.Progbar(X_test.shape[0])
        for X_batch, Y_batch in datagen.flow(X_test, X_test):
            score = model.test_on_batch(X_batch, X_batch.reshape(X_batch.shape[0],X_train.shape[2]**2*3))
            progbar.add(X_batch.shape[0], values=[("test loss", score)])
            
    model2 = Sequential()
    model2.add(encoder)
    codes = []
    targets = []
    model2.compile(loss = "mean_squared_error", optimizer = "sgd")
    for X_batch, Y_batch in datagen.flow(X_train, Y_train):
        codes.append(model2.predict(X_batch))
        targets.append(np.argmax(Y_batch))
        
    print('stack it...')
    codes = np.vstack(codes)
    targets = np.vstack(targets)
    print(codes.shape,'code shape')
Example #13
0
def cnn(**kwargs):
    verbose = kwargs.get("verbose", True)
    X_train = kwargs["X_train"]
    Y_train = kwargs["Y_train"]
    X_test = kwargs["X_test"]
    Y_test = kwargs["Y_test"]
    input_shape = kwargs["input_shape"]
    nb_classes = kwargs["nb_classes"]
    data_augmentation = kwargs.get("data_augmentation", True)

    nb_convo_layers = kwargs["nb_convo_layers"]
    nb_filters = kwargs["nb_filters"]
    nb_conv = kwargs["nb_conv"]

    convo_activations = kwargs["convo_activations"]
    maxpools = kwargs["maxpools"]
    pool_sizes = kwargs["pool_sizes"]
    convo_dropouts = kwargs["convo_dropouts"]

    nb_dense_layers = kwargs["nb_dense_layers"]
    dense_hidden_neurons = kwargs["dense_hidden_neurons"]
    dense_activations = kwargs["dense_activations"]
    dense_dropouts = kwargs["dense_dropouts"]

    loss = kwargs["loss"]
    optimizer = kwargs["optimizer"]
    nb_epoch = kwargs["nb_epoch"]
    batch_size = kwargs["batch_size"]

    model_file = kwargs.get("model_file")
    weights_file = kwargs.get("weights_file")
    results_file = kwargs.get("results_file")
    results = {
     "acc": [],
     "loss": [],
     "val_acc": [],
     "val_loss": []
    }

    # CNN architecture
    model = Sequential()

    # convolution layers
    for i in range(nb_convo_layers):
        # convo layer
        if i == 0:
            model.add(
                Convolution2D(
                    nb_filters[i],
                    nb_conv[i],
                    nb_conv[i],
                    input_shape=input_shape
                )
            )
        else:
            model.add(
                Convolution2D(
                    nb_filters[i],
                    nb_conv[i],
                    nb_conv[i],
                    border_mode='valid',
                )
            )

        # activation
        if convo_activations[i]:
            model.add(Activation(convo_activations[i]))

        # max-pooling
        if maxpools[i]:
            model.add(MaxPooling2D(pool_size=(pool_sizes[i], pool_sizes[i])))

        # dropout
        if convo_dropouts[i]:
            model.add(Dropout(convo_dropouts[i]))

    # dense layers
    model.add(Flatten())
    for i in range(nb_dense_layers):
        # dense layer
        if (i + 1) == nb_dense_layers:
            model.add(Dense(nb_classes))
        else:
            model.add(Dense(dense_hidden_neurons[i]))

        # activation
        if dense_activations[i]:
            model.add(Activation(dense_activations[i]))

        # dropout
        if dense_dropouts[i]:
            model.add(Dropout(dense_dropouts[i]))

    # loss function and optimizer
    if verbose:
        print("--> compiling CNN")
    model.compile(loss=loss, optimizer=optimizer)

    # fit model
    if verbose:
        print("--> fitting CNN")

    if data_augmentation is False:
        fitlog = model.fit(
            X_train,
            Y_train,
            batch_size=batch_size,
            nb_epoch=nb_epoch,
            show_accuracy=True,
            verbose=verbose,
            validation_data=(X_test, Y_test)
        )
        results = fitlog.history

    else:
        # turn on data augmentation
        datagen = ImageDataGenerator(
            featurewise_center=False,
            samplewise_center=True,
            featurewise_std_normalization=False,
            samplewise_std_normalization=False,
            zca_whitening=False,
            rotation_range=0,
            width_shift_range=0.1,
            height_shift_range=0.1,
            horizontal_flip=True,
            vertical_flip=True
        )
        datagen.fit(X_train)

        for e in range(nb_epoch):
            if verbose:
                print "epoch:", e

            tmp_train_acc = []
            tmp_train_loss = []
            tmp_test_acc = []
            tmp_test_loss = []
            train_batch_counter = 0
            test_batch_counter = 0

            for X_batch, Y_batch in datagen.flow(X_train, Y_train, batch_size):
                train_loss, train_accuracy = model.train_on_batch(
                    X_batch,
                    Y_batch,
                    accuracy=True
                )

                tmp_train_acc.append(train_accuracy)
                tmp_train_loss.append(train_loss)
                train_batch_counter += 1

            for X_batch, Y_batch in datagen.flow(X_test, Y_test, batch_size):
                valid_loss, valid_accuracy = model.test_on_batch(
                    X_batch,
                    Y_batch,
                    accuracy=True
                )
                tmp_test_acc.append(valid_accuracy)
                tmp_test_loss.append(valid_loss)
                test_batch_counter += 1

            epoch_train_acc = sum(tmp_train_acc) / float(train_batch_counter)
            epoch_train_loss = sum(tmp_train_loss) / float(train_batch_counter)
            epoch_test_acc = sum(tmp_test_acc) / float(test_batch_counter)
            epoch_test_loss = sum(tmp_test_loss) / float(test_batch_counter)

            results["acc"].append(epoch_train_acc)
            results["loss"].append(epoch_train_loss)
            results["val_acc"].append(epoch_test_acc)
            results["val_loss"].append(epoch_test_loss)

            if verbose:
                print "acc: {0}".format(epoch_train_acc),
                print "loss: {0}".format(epoch_train_loss),
                print "val_acc: {0}".format(epoch_test_acc),
                print "val_loss: {0}".format(epoch_test_loss)

    # save model
    if model_file:
        model_data = model.to_json()
        model_file = open(model_file, "w")
        model_file.write(json.dumps(model_data))
        model_file.close()

    # save model weights
    if weights_file:
        model.save_weights(weights_file, overwrite=True)

    # save results
    if results_file:
        results["nb_epoch"] = nb_epoch
        results["batch_size"] = batch_size
        rf = open(results_file, "w")
        rf.write(json.dumps(results))
        rf.close()

    # evaluate
    score = model.evaluate(
        X_test,
        Y_test,
        show_accuracy=True,
        verbose=verbose,
        batch_size=batch_size
    )

    return results, score
Example #14
0
def train_whale_data(**kwargs):
    print("analyzing whale data")
    # seed random
    np.random.seed(kwargs["seed"])

    # model inputs and parameters
    data_augmentation = kwargs.get("data_augmentation", False)
    X_train = kwargs["X_train"]
    X_test = kwargs["X_test"]
    Y_train = kwargs["Y_train"]
    Y_test = kwargs["Y_test"]

    img_rows = kwargs["img_rows"]
    img_cols = kwargs["img_cols"]

    nb_filters = kwargs.get("nb_filters", 32)
    nb_conv = kwargs.get("nb_conv", 3)
    nb_pool = kwargs.get("nb_pool", 2)

    batch_size = kwargs["batch_size"]
    nb_epoch = kwargs.get("nb_epoch", 12)
    nb_classes = kwargs.get("nb_classes", 10)

    model_file = kwargs["model_file"]
    weights_file = kwargs["weights_file"]
    results_file = kwargs["results_file"]

    # CNN architecture
    print("--> creating CNN network ...")
    results = {
        "acc": [],
        "val_acc": [],
        "loss": [],
        "val_loss": []
    }
    model = Sequential()

    # layer 1
    model.add(
        Convolution2D(
            nb_filters,
            nb_conv,
            nb_conv,
            input_shape=(1, img_rows, img_cols),
        )
    )
    model.add(Activation('relu'))
    model.add(Dropout(0.4))

    # layer 2
    model.add(
        Convolution2D(
            nb_filters,
            nb_conv,
            nb_conv,
            input_shape=(1, img_rows, img_cols),
        )
    )
    model.add(Activation('relu'))
    model.add(Dropout(0.4))

    # layer 3
    model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
    model.add(Dropout(0.4))

    # layer 4
    model.add(Flatten())
    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))

    # compile, fit and evaluate model
    print("--> compiling CNN functions")
    model.compile(
        loss='categorical_crossentropy',
        optimizer='sgd'
    )

    # fit model
    print("--> fitting CNN")
    if data_augmentation is False:
        print "--> fitting data"
        fitlog = model.fit(
            X_train,
            Y_train,
            batch_size=batch_size,
            nb_epoch=nb_epoch,
            show_accuracy=True,
            verbose=1,
            validation_data=(X_test, Y_test)
        )
        results = fitlog.history

    else:
        # turn on data augmentation
        print "--> augmenting data"
        datagen = ImageDataGenerator(
            featurewise_center=False,
            featurewise_std_normalization=False,
            rotation_range=20,
            width_shift_range=0.2,
            height_shift_range=0.2,
            horizontal_flip=True,
        )
        datagen.fit(X_train)

        print "--> fitting data"
        for e in range(nb_epoch):
            print "epoch:", e
            for X_batch, Y_batch in datagen.flow(X_train, Y_train, batch_size):
                train_loss, train_accuracy = model.train_on_batch(
                    X_batch,
                    Y_batch,
                    accuracy=True
                )
                valid_loss, valid_accuracy = model.test_on_batch(
                    X_test,
                    Y_test,
                    accuracy=True
                )

            results["acc"].append(float(train_accuracy))
            results["val_acc"].append(float(valid_accuracy))
            results["loss"].append(float(train_loss))
            results["val_loss"].append(float(valid_loss))

            print "acc: {0}".format(train_accuracy),
            print "val_acc: {0}".format(valid_accuracy),
            print "acc_loss: {0}".format(train_loss),
            print "val_loss: {0}".format(valid_loss)

    # save model
    model_data = model.to_json()
    model_file = open(model_file, "w")
    model_file.write(json.dumps(model_data))
    model_file.close()

    # save model weights
    model.save_weights(weights_file, overwrite=True)

    # save results
    results["nb_epoch"] = nb_epoch
    results["batch_size"] = batch_size
    rf = open(results_file, "w")
    rf.write(json.dumps(results))
    rf.close()
Example #15
0
for e in range(epoch_num):

    print 'Epoch #{}/{}'.format(e+1,epoch_num)
    sys.stdout.flush()

    shuffle(tr_it)


    for u in tqdm(tr_it):
        l,a=model.train_on_batch(tr_in[u],tr_out[u])
        tr_hist.r.addLA(l,a,tr_out[u].shape[0])
    # clear_output()
    tr_hist.log()

    for u in range(dev_in.shape[0]):
        l,a=model.test_on_batch(dev_in[u],dev_out[u])
        dev_hist.r.addLA(l,a,dev_out[u].shape[0])
    dev_hist.log()


    # for u in range(tst_in.shape[0]):
    #     l,a=model.test_on_batch(tst_in[u],tst_out[u])
    #     tst_hist.r.addLA(l,a,tst_out[u].shape[0])
    # tst_hist.log()


pickle.dump(model, open('models/classifier_enc.pkl','wb'))
pickle.dump(dev_hist, open('models/testHist_enc.pkl','wb'))
pickle.dump(tr_hist, open('models/trainHist_enc.pkl','wb'))

                                         window_size=4, negative_samples=test_labels, sampling_table=None)
 tag_couples, labels = sequence.tagged_cbows(seq, test_tag_seq, len(tag_tokenizer.word_index)+1, # replace seq with train_tag_seq
                                         window_size=4, negative_samples=test_labels, sampling_table=None)
 
 couples = np.array(couples, dtype="int32")
 tag_couples = np.array(tag_couples, dtype="int32")
 labels = np.array(labels)
 X_w = np.array(np.split(couples, len(seq)))
 X_t = np.array(np.split(tag_couples, len(seq)))
 if test_labels ==0 :
     # Divide number of examples to rank so that GPU does not cause out of memory error
     splitter = get_min_divisor(len(labels))
     test_y = np.reshape(np.empty_like(labels, dtype = 'float32'),(labels.shape[0],1))
     for j in range(splitter):
         test_loss, test_y_block = model.test_on_batch([X_w[:,j*(labels.shape[0]/splitter): (j+1)*(labels.shape[0]/splitter) ,:], 
                                                        X_t[:,j*(labels.shape[0]/splitter): (j+1)*(labels.shape[0]/splitter) ,:]],
                                                 labels[j*(labels.shape[0]/splitter): (j+1)*(labels.shape[0]/splitter)]) 
         test_y[j*(labels.shape[0]/splitter): (j+1)*(labels.shape[0]/splitter)] = test_y_block
 else:
     test_loss, test_y = model.test_on_batch([X_w, X_t], labels) 
 
 lraps.append(label_ranking_average_precision_score(np.reshape(np.array(labels),test_y.shape).T , test_y.T))
 mrr, recall, prec = test_utils.print_accuracy_results(np.array(labels) , np.reshape(test_y, np.array(labels).shape))
 mrrs.append(mrr)
 recalls.append(recall)
 precs.append(prec)
 losses.append(test_loss)
 test_losses.append(test_loss)
 if len(losses) % 100 == 0:
     progbar.update(i, values=[("loss", np.sum(losses))])
     losses = []
Example #17
0
'''


i=0
for file in fileList:
    print file
    print i
    x_train,y_train=readPklFile(file)
    #print x_train
    if x_train.ndim<3:
        continue
    if(len(x_train)==0):
        continue
    if i<1800:
        model.fit(x_train,y_train,nb_epoch=40,batch_size=32,verbose=1,show_accuracy=True)
    else:
        classes=model.predict(x_train,batch_size=32,verbose=1)
        y_test=Nomalization(classes)
        predictEntity.writelines(file+'\n'+"original:\n"+str(y_train)+"\npredict:\n"+str(y_test)+'\n')
        loss,acc=model.test_on_batch(x_train,y_train,accuracy=True)
        
        #print loss,acc
        #acc= CalculateAcc(y_train,y_test)
        FileEntity.writelines(file+'\n'+'loss: '+str(loss)+"\tacc: "+str(acc)+"\n")        
        
        #model.evaluate(x_train,y_train,batch_size=32,verbose=1,show_accuracy=True)
    i=i+1
FileEntity.close()
predictEntity.close()
print "finish"
	#TESTING GOES HERE
	print('Testing...')
	accuracyArray = list()
	lossArray = list()
	progbar = generic_utils.Progbar(X_test.shape[0])
	j = 0
	while(j != sizeTest):
		if(j+batch_size >= sizeTest):
			batchToTest = model2.predict(X_test[j:sizeTest])
			labelOfBatchToTest = Y_test[j:sizeTest,:]
			j = sizeTest
		else:
			batchToTest = model2.predict(X_test[j:j+batch_size])
			labelOfBatchToTest = Y_test[j:j+batch_size,:]
			j += batch_size
		score = model3.test_on_batch(batchToTest, labelOfBatchToTest,accuracy=True)
		lossArray.append(score[0])
		accuracyArray.append(score[1])
		progbar.add(batchToTest.shape[0], values=[('test loss', score[0]),('test accuracy',score[1])])
	lossIteration2.append(np.mean(lossArray))
	accuracyIteration2.append(np.mean(accuracyArray)) 

weightsLayer2 = model3.layers[0].get_weights()

print('ITERATION 2 STARTING (FW)')

#PREVIOUS MODEL
model2 = Sequential()
model2.add(Convolution2D(96, nb_conv, nb_conv,
                        input_shape=(1, img_rows, img_cols),weights=weightsLayer1))
model2.add(Activation('relu'))
Example #19
0
    print('Loss = {}, Precision = {}, Recall = {}, F1 = {}'.format(avgLoss, con_dict['r'], con_dict['p'], con_dict['f1']))
    
    
    print("Validating =>")
    
    val_pred_label = []
    avgLoss = 0
    
    bar = progressbar.ProgressBar(max_value=len(val_x))
    for n_batch, sent in bar(enumerate(val_x)):
        label = val_label[n_batch]
        label = np.eye(n_classes)[label][np.newaxis,:]
        sent = sent[np.newaxis,:]
        
        if sent.shape[1] > 1: #some bug in keras
            loss = model.test_on_batch(sent, label)
            avgLoss += loss

        pred = model.predict_on_batch(sent)
        pred = np.argmax(pred,-1)[0]
        val_pred_label.append(pred)

    avgLoss = avgLoss/n_batch
    
    predword_val = [ list(map(lambda x: idx2la[x], y)) for y in val_pred_label]
    con_dict = conlleval(predword_val, groundtruth_val, words_val, 'r.txt')
    val_f_scores.append(con_dict['f1'])
    
    print('Loss = {}, Precision = {}, Recall = {}, F1 = {}'.format(avgLoss, con_dict['r'], con_dict['p'], con_dict['f1']))

    if con_dict['f1'] > best_val_f1:
Example #20
0
                X, y = get_data(files[n:n+batch_size], n)
            else:
                X, y = get_data(files[n:], n)

            X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)  # , random_state=0
            X_train = np.array(X_train)
            X_test = np.array(X_test)
            y_train = np.array(y_train)
            y_test = np.array(y_test)

            # convert class vectors to binary class matrices
            Y_train = np_utils.to_categorical(y_train, nb_classes)
            Y_test = np_utils.to_categorical(y_test, nb_classes)

            model.train_on_batch(X_train, Y_train)
            l, a = model.test_on_batch(X_test, Y_test)

            acc.append(a)
            loss.append(l)
        print "Epoch: %s <==> Val_loss: %s <> Val_acc: %s "% ( str(e), str(sum(loss) / len(loss)), str(sum(acc) / len(acc)) )

'''
fill between on open and close included:
Epoch: 0 <==> Val_loss: 1.19601917664 <> Val_acc: 0.491666666667
Epoch: 1 <==> Val_loss: 0.895532437166 <> Val_acc: 0.441666666667
Epoch: 2 <==> Val_loss: 0.89461884896 <> Val_acc: 0.408333333333
Epoch: 3 <==> Val_loss: 0.900162645181 <> Val_acc: 0.433333333333
Epoch: 4 <==> Val_loss: 0.908274920781 <> Val_acc: 0.4
Epoch: 5 <==> Val_loss: 0.906020263831 <> Val_acc: 0.45
Epoch: 6 <==> Val_loss: 0.910774775346 <> Val_acc: 0.475
Epoch: 7 <==> Val_loss: 0.922956418991 <> Val_acc: 0.45
def analyze_whale_data(**kwargs):
    print("analyzing whale data")
    # seed random
    np.random.seed(kwargs["seed"])

    # model inputs and parameters
    #X_train = kwargs["X_train"]
    #X_test = kwargs["X_test"]
    #Y_train = kwargs["Y_train"]
    #Y_test = kwargs["Y_test"]
    
    X_train=np.load(home+'/gabor/numpyFiles/Training Set.npy')
    X_test=np.load(home+'/gabor/numpyFiles/TestSet.npy')
    Y_train=np.load(home+'/gabor/numpyFiles/Training Labels.npy')
    Y_test=np.load(home+'/gabor/numpyFiles/TestSet Labels.npy')
    
    X_test = X_test.reshape(-1, 1, 30, 96)
    Y_test = np_utils.to_categorical(Y_test, 447)
    
    
    X_train = X_train.reshape(-1, 1, 30, 96)
    Y_train = np_utils.to_categorical(Y_train, 447)
    
    img_rows = kwargs["img_rows"]
    img_cols = kwargs["img_cols"]

    nb_filters = kwargs.get("nb_filters", 32)
    nb_conv = kwargs.get("nb_conv", 3)
    nb_pool = kwargs.get("nb_pool", 2)

    batch_size = kwargs.get("batch_size", 32)
    nb_epoch = kwargs.get("nb_epoch", 12)
    nb_classes = kwargs.get("nb_classes", 447)

    print("X_test.shape == {};".format(X_test.shape))
    print("Y_test.shape == {};".format(Y_test.shape))
    print("X_test.shape == {};".format(X_train.shape))
    print("Y_test.shape == {};".format(Y_train.shape))
    
    # CNN architecture
    print("--> creating CNN network ...")
    model = Sequential()

    # layer 1
    model.add(

        Convolution2D(
            nb_filters,
            nb_conv,
            nb_conv,
            border_mode='full',
            input_shape=(1, img_rows, img_cols)
        )
    )
    model.add(Activation('relu'))

    # layer 2
    model.add(Convolution2D(32, 5, 5, border_mode='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))

    # layer 3
    model.add(Convolution2D(64, 5, 5, border_mode='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
    
    # layer 4
    model.add(Convolution2D(128, 3, 3, border_mode='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
    
    # layer 5
    model.add(Flatten())
    model.add(Dense(1000))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    
    # layer 6
    model.add(Dense(1000))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    # layer 4
    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))

    # compile, fit and evaluate model
    print("--> compiling CNN functions")
    model.compile(
        loss='categorical_crossentropy',
        optimizer='adadelta'
    )
    print("--> fitting CNN")
    datagen = ImageDataGenerator(
        featurewise_center=False,  # set input mean to 0 over the dataset
        samplewise_center=False,  # set each sample mean to 0
        featurewise_std_normalization=False,  # divide inputs by std of the dataset
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # apply ZCA whitening
        rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=True)  # randomly flip images
    
    datagen.fit(X_train)
    
    loss1 = np.zeros((nb_epoch))
    loss1 = loss1.astype(np.float32)
    acc1 = np.zeros((nb_epoch))
    acc1 = acc1.astype(np.float32)
    score1 = np.zeros((nb_epoch))
    score1 = score1.astype(np.float32)
    test_acc1 = np.zeros((nb_epoch))
    test_acc1 = test_acc1.astype(np.float32)    
    
    for e in range(nb_epoch):
        print('-'*40)
        print('Epoch', e)
        print('-'*40)
        print("Training...")
        # batch train with realtime data augmentation
        progbar = generic_utils.Progbar(X_train.shape[0])
        for X_batch, Y_batch in datagen.flow(X_train, Y_train):
            loss, acc = model.train_on_batch(X_batch, Y_batch, accuracy=True)
            progbar.add(X_batch.shape[0], values=[("train loss", loss), ("train accuracy", acc)])
        loss1[e] = loss
        acc1[e] = acc

        print("Testing...")
        # test time!
        progbar = generic_utils.Progbar(X_test.shape[0])
        for X_batch, Y_batch in datagen.flow(X_test, Y_test):
            score, test_acc = model.test_on_batch(X_batch, Y_batch, accuracy=True)
            progbar.add(X_batch.shape[0], values=[("test loss", score), ("test accuracy", test_acc)])
        score1[e] = score
        test_acc1[e] = test_acc
    
    print("--> saving CNN")
    json_string = model.to_json()
    open('my_model_architecture.json', 'w').write(json_string)
    model.save_weights('my_model_weights.h5') 
    
    return (loss1, acc1, score1, test_acc1)
Example #22
0
class Model() :

    # mode = 'encode' || 'train'
    def __init__(self, mode) :
        self.io_dim = Codec.n_chars
        with open('config/semantic.json') as file :
            semantic_config = json.load(file)
            self.feature_dim = semantic_config['vectorDim']
            self.seq_len = semantic_config['seqLength']
        self.mode = mode
        self.model = SeqModel()
        self.encoder = SeqContainer()
        self.encoder.add(TimeDistributedDense(
            input_dim = self.io_dim,
            input_length = self.seq_len,
            output_dim = self.feature_dim,
            activation = 'sigmoid'))
        self.encoder.add(GRU(
            input_dim = self.feature_dim,
            input_length = self.seq_len,
            output_dim = self.feature_dim,
            activation = 'sigmoid',
            inner_activation = 'hard_sigmoid',
            truncate_gradient = self.seq_len,
            return_sequences = True))
        self.encoder.add(GRU(
            input_dim = self.feature_dim,
            input_length = self.seq_len,
            output_dim = self.feature_dim,
            activation = 'sigmoid',
            inner_activation = 'hard_sigmoid',
            truncate_gradient = self.seq_len,
            return_sequences = False))
        self.model.add(self.encoder)
        if mode == 'train' :
            self.decoder = SeqContainer()
            self.decoder.add(SimpleRNN(
                input_dim = self.feature_dim,
                input_length = self.seq_len,
                output_dim = self.feature_dim,
                activation = 'sigmoid',
                truncate_gradient = self.seq_len,
                return_sequences = True))
            self.decoder.add(TimeDistributedDense(
                input_dim = self.feature_dim,
                input_length = self.seq_len,
                output_dim = self.io_dim,
                activation = 'sigmoid'))
            self.model.add(RepeatVector(self.seq_len, input_shape = (self.feature_dim,)))
            self.model.add(self.decoder)

    def _load_weights(self, path) :
        with h5py.File(path, 'r') as file :
            group = file['/weights']
            n_layers = group.attrs.get('n_layers')[0]
            weights = []
            for i in range(n_layers) :
                layer_weights = file['/weights/layer_' + str(i)][()]
                weights.append(layer_weights)
            return weights

    def load(self) :
        encoder_weights = self._load_weights('data/encoder.hdf5')
        self.encoder.set_weights(encoder_weights)
        if self.mode == 'train' :
            decoder_weights = self._load_weights('data/decoder.hdf5')
            self.decoder.set_weights(decoder_weights)

    def _save_weights(self, weights, path) :
        with h5py.File(path, 'w') as file :
            group = file.create_group('weights')
            n_layers = len(weights)
            group.attrs.create('n_layers', np.array([n_layers]))
            for i, layer_weights in enumerate(weights) :
                group.create_dataset('layer_' + str(i), data = layer_weights)

    def save(self) :
        if self.mode != 'train' :
            raise Exception('invalid mode')
        encoder_weights = self.encoder.get_weights()
        decoder_weights = self.decoder.get_weights()
        self._save_weights(encoder_weights, 'data/encoder.hdf5')
        self._save_weights(decoder_weights, 'data/decoder.hdf5')

    def compile(self) :
        self.model.compile(loss = 'categorical_crossentropy', optimizer = Adadelta(clipnorm = 1.))

    # in_data & out_data numpy bool array of shape (n_sample, seq_len, io_dim)
    # return train (loss, accuracy)
    def train(self, in_data, out_data) :
        if self.mode != 'train' :
            raise Exception('invalid mode')
        return self.model.train_on_batch(in_data, out_data, accuracy = True)

    # in_data & out_data numpy bool array of shape (n_sample, seq_len, io_dim)
    # return the evaluation (loss, accuracy)
    def evaluate(self, in_data, out_data) :
        if self.mode != 'train' :
            raise Exception('invalid mode')
        return self.model.test_on_batch(in_data, out_data, accuracy = True)

    # sequence : numpy bool array of shape (seq_len, io_dim)
    # return : numpy float32 array of shape (feature_dim)
    def encode(self, sequence) :
        if self.mode != 'encode' :
            raise Exception('invalid mode')
        input_sequences = np.ndarray((1, self.seq_len, self.io_dim), dtype = np.bool)
        input_sequences[0] = sequence
        return self.model.predict(input_sequences)[0]
Example #23
0
    print('Test score:', score)

else:
    print('Using real time data augmentation')
    for e in range(nb_epoch):
        print('-'*40)
        print('Epoch', e)
        print('-'*40)
        print('Training...')
        # batch train with realtime data augmentation
        progbar = generic_utils.Progbar(num_images-test_size)
        for X_batch, Y_batch in flow(image_list[0:-test_size]):
            X_batch = X_batch.reshape(X_batch.shape[0], 3, img_rows, img_cols)
            Y_batch = np_utils.to_categorical(Y_batch, nb_classes)
            loss = model.train_on_batch(X_batch, Y_batch)
            progbar.add(X_batch.shape[0], values=[('train loss', loss)])

        print('Testing...')
        # test time!
        progbar = generic_utils.Progbar(test_size)
        for X_batch, Y_batch in flow(image_list[-test_size:]):
            X_batch = X_batch.reshape(X_batch.shape[0], 3, img_rows, img_cols)
            Y_batch = np_utils.to_categorical(Y_batch, nb_classes)
            score = model.test_on_batch(X_batch, Y_batch)
            progbar.add(X_batch.shape[0], values=[('test loss', score)])

    json_string = model.to_json()
    open('cnn1_model_architecture.json', 'w').write(json_string)
    model.save_weights('cnn1_model_weights.h5')

Example #24
0
    index += 1

model.save_weights("test_weights.hdf5", overwrite=True)
model.load_weights("test_weights.hdf5")

end = datetime.now()
diff = end - begin
avgSec = diff.total_seconds() / EPOCHS
avgMin = int(avgSec / 60)
avgHour = int(avgMin / 60)
avgDay = int(avgHour / 24)
avgSec -= 60 * avgMin
avgMin -= 60 * avgHour
avgHour -= 24 * avgDay

# loss, acc = model.evaluate(tX, tY, batch_size=BATCH_SIZE, show_accuracy=True)
progbar = generic_utils.Progbar(tX.shape[0])
index = 0
for i in range(len(tX) / BATCH_SIZE):
    loss, acc = model.test_on_batch(tX[index : index + BATCH_SIZE], tY[index : index + BATCH_SIZE], accuracy=True)
    progbar.add(BATCH_SIZE, values=[("test loss", loss), ("test acc", acc)])
    index += BATCH_SIZE

"""
fResult = open('test.txt', 'a+')
fResult.write('Test loss / test accuracy = %.4f / %.4f\n'%(loss, acc))
fResult.write('mode acc / test mode acc = %.4f / %.4f\n'%(accmode, taccmode))
fResult.write('Average learning time = %ddays %d:%d:%d\n\n'%(avgDay, avgHour, avgMin, avgSec))
fResult.close()
"""
def CNN():
	input_frames=10
	batch_size = 32
	nb_classes = 20
	nb_epoch = 200
	img_rows, img_cols = 224,224
	img_channels = 2*input_frames

	print 'X_sample: '+str(X_sample.shape)
	print 'X_test: '+str(X_test.shape)
	print 'Y_test: '+str(Y_test.shape)


	print 'Preparing architecture...'

	model = Sequential()

	model.add(Convolution2D(96, 7, 7, border_mode='same',input_shape=(img_channels, img_rows, img_cols)))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2)))

	model.add(Convolution2D(256, 5, 5, border_mode='same'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2)))

	model.add(Convolution2D(512, 3, 3, border_mode='same'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))

	model.add(Convolution2D(512, 3, 3, border_mode='same'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))

	model.add(Convolution2D(512, 3, 3, border_mode='same'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2)))

	model.add(Flatten())
	model.add(Dense(2048))
	fc_output=Activation('relu')
	model.add(fc_output)
	model.add(Dropout(0.5))
	model.add(Dense(2048))
	model.add(Activation('relu'))
	model.add(Dropout(0.5))

	model.add(Dense(nb_classes))
	softmax_output=Activation('softmax')
	model.add(softmax_output)



	print 'Starting with training...'
	gc.collect()
	sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
	model.compile(loss={'output1':'categorical_crossentropy'}, optimizer=sgd)

	print("Using real time data augmentation")

	datagen = ImageDataGenerator(
		featurewise_center=True,  # set input mean to 0 over the dataset
		samplewise_center=False,  # set each sample mean to 0
		featurewise_std_normalization=True,  # divide inputs by std of the dataset
		samplewise_std_normalization=False,  # divide each input by its std
		zca_whitening=False,  # apply ZCA whitening
		rotation_range=20,  # randomly rotate images in the range (degrees, 0 to 180)
		width_shift_range=0.2,  # randomly shift images horizontally (fraction of total width)
		height_shift_range=0.2,  # randomly shift images vertically (fraction of total height)
		horizontal_flip=True,  # randomly flip images
		vertical_flip=True)  # randomly flip images

	# compute quantities required for featurewise normalization
	# (std, mean, and principal components if ZCA whitening is applied)
	datagen.fit(X_sample)

	for e in range(nb_epoch):
		print('-'*40)
		print('Epoch', e)
		print('-'*40)
		print("Training...")
		# batch train with realtime data augmentation
		progbar = generic_utils.Progbar(X_train.shape[0])
		for X_train, Y_train in getTrainData():
			for X_batch, Y_batch in datagen.flow(X_train, Y_train, batch_size=batch_size):
				loss = model.train_on_batch(X_batch, Y_batch, accuracy=True)
				progbar.add(X_batch.shape[0], values=[("train loss", loss[0]),("train accuracy", loss[1])])
				fc_output
				softmax_output

		print('Saving layer representation and saving weights...')

		with h5py.File('fc_output.h5', 'w') as hf:
			hf.create_dataset('fc_output', data=fc_output)

		with h5py.File('softmax_output.h5', 'w') as hf:
			hf.create_dataset('softmax_output', data=softmax_output)

		model.save_weights('temporal_stream_model.h5')

		print("Testing...")
		# test time!
		progbar = generic_utils.Progbar(X_test.shape[0])
		for X_test, Y_test in getTestData():
			for X_batch, Y_batch in datagen.flow(X_test, Y_test, batch_size=batch_size):
				score = model.test_on_batch(X_batch, Y_batch, accuracy=True)
				progbar.add(X_batch.shape[0], values=[("test loss", score[0]),("test accuracy", score[1])])
Example #26
0
            height_shift_range=0,  # randomly shift images vertically (fraction of total height)
            horizontal_flip=False,  # randomly flip images
            vertical_flip=False)  # randomly flip images

        # compute quantities required for featurewise normalization
        # (std, mean, and principal components if ZCA whitening is applied)
        datagen.fit(X_train)

        for e in range(nb_epoch):
            print('-'*40)
            print('Epoch', e)
            print('-'*40)
            print("Training...")
            # batch train with realtime data augmentation
            progbar = generic_utils.Progbar(X_train.shape[0])
            for X_batch, Y_batch in datagen.flow(X_train, Y_train, batch_size=batch_size):
                score, trainAccu = model.train_on_batch(X_batch, Y_batch, accuracy=True)
                progbar.add(X_batch.shape[0], values=[("train accuracy", trainAccu)])

            print("Testing...")
            # test time!
            progbar = generic_utils.Progbar(X_test.shape[0])
            for X_batch, Y_batch in datagen.flow(X_test, Y_test, batch_size=batch_size):
                score, testAccu = model.test_on_batch(X_batch, Y_batch, accuracy=True)
                progbar.add(X_batch.shape[0], values=[("test accuracy", testAccu)])
    trainScores.append(trainAccu)
    testScores.append(testAccu)
scipy.io.savemat('cnn_results', {'trainAccu': trainScores, 'testAccu': testScores})
print ('Average train accuracies: {0}'.format(np.mean(trainScores)))
print ('Average test accuracies: {0}'.format(np.mean(testScores)))