示例#1
0
文件: main.py 项目: onlyarche/imageQA
def main():

    os.chdir('/home/seonhoon/Desktop/workspace/ImageQA/data/')

    n_vocab = 12047
    y_vocab = 430
    dim_word = 1024
    dim = 1024
    dim_ctx = 512
    maxlen = 60

    train = pd.read_pickle('train.pkl')
    #   train, valid = train_test_split(train, test_size=0.2)
    valid = pd.read_pickle('test.pkl')

    train_x = [q for q in train['q']]
    train_y = [a[0] for a in train['a']]
    train_y = np.array(train_y)[:, None]
    train_y = np_utils.to_categorical(train_y, y_vocab).astype('int32')
    train_x, train_x_mask = prepare_data(train_x, maxlen)

    img_folder = '/home/seonhoon/Desktop/workspace/ImageQA/data/images/'
    train_imgs = train['img_id'].apply(
        lambda x: img_folder + 'train/' + x + '.jpg')
    train_imgs = train_imgs.tolist()

    valid_x = [q for q in valid['q']]
    valid_y = [a[0] for a in valid['a']]
    valid_x, valid_x_mask = prepare_data(valid_x, maxlen)

    valid_imgs = valid['img_id'].apply(
        lambda x: img_folder + 'test/' + x + '.jpg')
    valid_imgs = valid_imgs.tolist()

    print 'train x :', train_x.shape
    print 'train x_mask:', train_x_mask.shape
    print 'train y : ', train_y.shape
    print 'train imgs :', len(train_imgs)

    print 'valid x :', valid_x.shape
    print 'valid x_mask:', valid_x_mask.shape
    #print 'valid y : ', valid_y.shape
    print 'valid imgs :', len(valid_imgs)

    model = BIRNN_GRU(n_vocab, y_vocab, dim_word, dim, dim_ctx)

    model.train(train_x,
                train_x_mask,
                train_imgs,
                train_y,
                valid_x,
                valid_x_mask,
                valid_imgs,
                valid_y,
                valid=2,
                lr=0.008,
                dropout=0.4,
                batch_size=512,
                epoch=60,
                save=4)
示例#2
0
def main():


    y_class = 10
    dim_freq = 5512
    dim = 1024
    step=112
    dim_ctx = 4096
    
    os.chdir('/home/seonhoon/Desktop/workspace/music/data6/')
    train=pd.read_pickle('train_fft.pkl')
    valid=pd.read_pickle('valid_fft.pkl')
    train_x=np.array([ q for q in train['x'] ]).reshape(-1,step,dim_freq).astype('float32')
    train_y=[ a for a in train['y'] ]
    train_y=np.array(train_y)[:,None]
    train_y = np_utils.to_categorical(train_y, y_class).astype('int32')
    train_img = np.array([ f for f in train['cnn_feature'] ]).astype('float32')

    
    valid_x=np.array([ q for q in valid['x'] ]).reshape(-1,step,dim_freq).astype('float32')
    valid_y=[ a for a in valid['y'] ]
    valid_y_true = valid_y
    valid_y=np.array(valid_y)[:,None]
    valid_y_original=valid_y
    valid_y = np_utils.to_categorical(valid_y, y_class).astype('int32')
    valid_img = np.array([ f for f in valid['cnn_feature'] ]).astype('float32')

    print 'train x :', train_x.shape
    print 'train y : ', train_y.shape
    print 'train imgs :', train_img.shape

    print 'valid x :', valid_x.shape
    print 'valid y : ', valid_y.shape
    print 'valid imgs :', valid_img.shape
    


    
    model = BIRNN_GRU(y_class, dim_freq, dim, dim_ctx)
 
    model.train(train_x, train_img, train_y, 
                valid_x, valid_img, valid_y_original, valid=1,
                lr=0.0001, dropout=0.8, batch_size=512, epoch=100, save=10)

    prediction, probs = model.prediction(valid_x, valid_img, valid_y, lr=0.0001, batch_size=512)

    print valid_y_true
    print prediction
    correct = 0 
    for i in xrange(len(valid_y_true)):
        if valid_y_true[i]==prediction[i]:
            correct += 1
    print correct
    
    true = pd.DataFrame(valid_y_true) 
    pred = pd.DataFrame(prediction) 
    true.to_pickle('/home/seonhoon/Desktop/workspace/music/data6/true.pkl')
    pred.to_pickle('/home/seonhoon/Desktop/workspace/music/data6/pred.pkl')
    draw_cm(valid_y_true, prediction)
示例#3
0
def main():
    
    



    os.chdir('/home/seonhoon/Desktop/workspace/ImageQA/data/')

    n_vocab = 12047
    y_vocab = 430
    dim_word = 1024
    dim = 1024
    dim_ctx = 512
    maxlen = 60
    
    train=pd.read_pickle('train.pkl')
 #   train, valid = train_test_split(train, test_size=0.2)
    valid=pd.read_pickle('test.pkl')  
    
    train_x=[ q for q in train['q'] ]
    train_y=[ a[0] for a in train['a'] ]
    train_y=np.array(train_y)[:,None]
    train_y = np_utils.to_categorical(train_y, y_vocab).astype('int32')
    train_x , train_x_mask = prepare_data(train_x, maxlen)


    img_folder='/home/seonhoon/Desktop/workspace/ImageQA/data/images/'
    train_imgs = train['img_id'].apply(lambda x : img_folder+'train/'+x+'.jpg')
    train_imgs = train_imgs.tolist()
    
    valid_x=[ q for q in valid['q'] ]
    valid_y=[ a[0] for a in valid['a'] ]
    valid_x , valid_x_mask = prepare_data(valid_x, maxlen)

    valid_imgs = valid['img_id'].apply(lambda x : img_folder+'test/'+x+'.jpg')
    valid_imgs = valid_imgs.tolist()
    
    print 'train x :', train_x.shape
    print 'train x_mask:', train_x_mask.shape
    print 'train y : ', train_y.shape
    print 'train imgs :', len(train_imgs)
    
    print 'valid x :', valid_x.shape
    print 'valid x_mask:', valid_x_mask.shape
    #print 'valid y : ', valid_y.shape
    print 'valid imgs :', len(valid_imgs)

    model = BIRNN_GRU(n_vocab, y_vocab, dim_word, dim, dim_ctx)

    model.train(train_x, train_x_mask, train_imgs, train_y, 
                valid_x, valid_x_mask, valid_imgs, valid_y, valid=2,
                lr=0.008, dropout=0.4, batch_size=512, epoch=60, save=4)