def main(num_epochs=NUM_EPOCHS):
    print("Loading data...")
    load_path = '/idiap/user/dwu/spyder/Codalab_Age_estimation/'
    fname = load_path+"data_with_label_DPM_Color_NoCROP.hdf5"
    X, y, y_mean, y_std, y_train_variance, X_mean, X_std = load(fname)
    dataloader = DataLoader(X, y, validation_num=VALIDATIONNUM, batch_size=BATCH_SIZE)

    if True:
        print("Building model and compiling functions...")
        output_layer = build_model()
        x_ = _shared(np.empty((BATCH_SIZE,3,96,96)))
        y_ = _shared(np.empty((BATCH_SIZE,1)))

        iter_funcs = create_iter_functions(
            x_, y_,
            output_layer,
            X_tensor_type=T.tensor4,
            learning_rate=LEARNING_RATE
        )

    if True:
        print("Loading the trained parameters")
        params_all = pickle.load(open('Lasagne_age_regressor_E200.pkl', 'rb'))
        lasagne.layers.set_all_param_values(output_layer, params_all)

    print("Starting training...")
    now = time.time()
    valid_loss = np.inf
    try:
        for epoch in train(iter_funcs, dataloader, x_, y_):
            print("Epoch {} of {} took {:.3f}s".format(
                epoch['number'], num_epochs, time.time() - now))
            now = time.time()
            print("  training loss:\t\t{:.6f}".format(epoch['train_loss']))
            print("  validation loss:\t\t{:.6f}".format(epoch['valid_loss']))

            if epoch['number'] >= num_epochs:
                break
            # we also save the best validation parameters:
            if epoch['valid_loss'] < valid_loss:
                valid_loss =  epoch['valid_loss']
                print "saving best validation parameters with best parameter epoch: "+str(epoch['number'])
                all_params = lasagne.layers.get_all_param_values(output_layer)
                saved_name = 'Lasagne_age_regressor_best.pkl'
                with(open(saved_name, 'wb')) as file:
                    pickle.dump(all_params, file)


    except KeyboardInterrupt:
        all_params = lasagne.layers.get_all_param_values(output_layer)
        with(open('Lasagne_age_regressor.pkl', 'wb')) as file:
            pickle.dump(all_params, file)

    all_params = lasagne.layers.get_all_param_values(output_layer)
    with(open('Lasagne_age_regressor.pkl', 'wb')) as file:
        pickle.dump(all_params, file)
    print "best parameter epoch: "+str(epoch['number'])
    return output_layer
コード例 #2
0
def main():
    print("Loading data...")
    load_path = '/idiap/user/dwu/spyder/Codalab_Age_estimation/'
    fname = load_path+"data_with_label_DPM_Color_NoCROP.hdf5"
    X, y, y_mean, y_std, y_train_variance, X_mean, X_std = load(fname, test=True)
    dataloader = DataLoaderTest(X)



    print("Building model and compiling functions...")
    output_layer = build_model(input_width=96, input_height=96, output_dim=1,
                batch_size=BATCH_SIZE, dimshuffle=True)
    #output_layer = build_model()
    print("Loading the trained parameters")
    params_all = pickle.load(open('Lasagne_age_regressor_best.pkl', 'rb'))
    lasagne.layers.set_all_param_values(output_layer, params_all)

    x_ = _shared(np.empty((BATCH_SIZE,3,96,96)))

    iter_funcs = create_test_functions(x_, output_layer, X_tensor_type=T.tensor4)

    now = time.time()
    predAges = np.empty(shape=(dataloader.X.shape[0],1))

    valid_image_list = '/idiap/user/dwu/spyder/Codalab_Age_estimation/valid_matlab_squence.csv'
    Valid_image_name = []
    with open(valid_image_list, 'r') as valid_list_file:
        for image_name in valid_list_file:
            #print image_name[:-1]
            Valid_image_name.append(image_name[:-1])

    image_count = 0
    print "start predicting"
    for b in range(dataloader.n_iter_test):
        dataloader.next_test_batch(x_)
        predAge_batch = iter_funcs()
        #print predAge_batch.shape
        predAges[b*BATCH_SIZE:b*BATCH_SIZE+predAge_batch.shape[0],:] = predAge_batch * y_std + y_mean

        if True:
            #let's save all the faces
            valid_image_dir = '/idiap/user/dwu/spyder/Codalab_Age_estimation/Validation'
            valid_save_dir = '/idiap/user/dwu/spyder/Codalab_Age_estimation/Valid_crop'
            font = cv2.FONT_HERSHEY_SIMPLEX
            for index in range(len(predAge_batch)):
                color_image = cv2.imread(os.path.join(valid_image_dir, Valid_image_name[image_count+index]))
                cv2.putText(color_image, str(predAge_batch[index] * y_std + y_mean), (0,50), font, 1, (255,0,255))
                cv2.imwrite(os.path.join(valid_save_dir, Valid_image_name[image_count+index]), color_image)
            image_count += BATCH_SIZE

    y_dict = {}
    y_dict['y_pred'] = predAges
    with open(load_path+'valid_y_pred.pkl','wb') as handle:
        pickle.dump(y_dict, handle)
    print("Finish predicting {} images, using time {:.3f}s".format(dataloader.X.shape[0], time.time()-now))
コード例 #3
0
def main():
    print("Loading data...")
    load_path = '/idiap/user/dwu/spyder/Codalab_Age_estimation/'
    fname = load_path + "data_with_label_DPM_Color_NoCROP.hdf5"
    X, y, y_mean, y_std, y_train_variance, X_mean, X_std = load(fname,
                                                                test=True)
    dataloader = DataLoaderTest(X)

    print("Building model and compiling functions...")
    output_layer = build_model(input_width=96,
                               input_height=96,
                               output_dim=1,
                               batch_size=BATCH_SIZE,
                               dimshuffle=True)
    #output_layer = build_model()
    print("Loading the trained parameters")
    params_all = pickle.load(open('Lasagne_age_regressor_best.pkl', 'rb'))
    lasagne.layers.set_all_param_values(output_layer, params_all)

    x_ = _shared(np.empty((BATCH_SIZE, 3, 96, 96)))

    iter_funcs = create_test_functions(x_,
                                       output_layer,
                                       X_tensor_type=T.tensor4)

    now = time.time()
    predAges = np.empty(shape=(dataloader.X.shape[0], 1))

    valid_image_list = '/idiap/user/dwu/spyder/Codalab_Age_estimation/valid_matlab_squence.csv'
    Valid_image_name = []
    with open(valid_image_list, 'r') as valid_list_file:
        for image_name in valid_list_file:
            #print image_name[:-1]
            Valid_image_name.append(image_name[:-1])

    image_count = 0
    print "start predicting"
    for b in range(dataloader.n_iter_test):
        dataloader.next_test_batch(x_)
        predAge_batch = iter_funcs()
        #print predAge_batch.shape
        predAges[b * BATCH_SIZE:b * BATCH_SIZE +
                 predAge_batch.shape[0], :] = predAge_batch * y_std + y_mean

        if True:
            #let's save all the faces
            valid_image_dir = '/idiap/user/dwu/spyder/Codalab_Age_estimation/Validation'
            valid_save_dir = '/idiap/user/dwu/spyder/Codalab_Age_estimation/Valid_crop'
            font = cv2.FONT_HERSHEY_SIMPLEX
            for index in range(len(predAge_batch)):
                color_image = cv2.imread(
                    os.path.join(valid_image_dir,
                                 Valid_image_name[image_count + index]))
                cv2.putText(color_image,
                            str(predAge_batch[index] * y_std + y_mean),
                            (0, 50), font, 1, (255, 0, 255))
                cv2.imwrite(
                    os.path.join(valid_save_dir,
                                 Valid_image_name[image_count + index]),
                    color_image)
            image_count += BATCH_SIZE

    y_dict = {}
    y_dict['y_pred'] = predAges
    with open(load_path + 'valid_y_pred.pkl', 'wb') as handle:
        pickle.dump(y_dict, handle)
    print("Finish predicting {} images, using time {:.3f}s".format(
        dataloader.X.shape[0],
        time.time() - now))