shuffle = True show_accuracy = True MODEL_ROOT = '../models/elephas/' PREDICTION_ROOT = '../predictions/' MODEL = 'sixlayeradagrad_noLSTM_batch_{}'.format(batch_size) print 'Loading data...' (X_train,Y_train),(X_test,Y_test) = load_data() TEST_ID = '../Data/pkl/img_q_id_test' TEST_ID_PKL = pickle.load(open(TEST_ID+'.pkl','rb')) ids = map(nameToId,[ TEST_ID_PKL[idx][1] for idx in range(len(TEST_ID_PKL)) ]) print 'Building model...' model = Akar.keras_model(1) #print 'Defining callbacks...' #checkpoint = ModelCheckpoint('../models/elephas/checkpoint_'+MODEL+'.h5', monitor='val_loss', verbose=0, save_best_only=True, mode='auto') #earlystopping = EarlyStopping(monitor='val_loss', patience=2, verbose=0) print 'Start training...' for epoch in [20,40,60,80,100,120,140,160,180,200]: model.fit( X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=verbose, callbacks=[],validation_split=validation_split, shuffle=shuffle,show_accuracy=show_accuracy) model.save_weights(MODEL_ROOT+MODEL+"_{}.h5".format(epoch)) probs = model.predict(X_test,batch_size=128) answers = map(numToC,np.argmax(probs,axis=1).tolist()) prediction = zip(ids,answers)
for num in xrange(len(IM_ID)): ID = IM_ID[num].split('_')[2].split('.')[0] IM_ID_DICT[ID] = num mem_shape = (40504, 1, 1000) mem_image = np.memmap('../Data/val2014/vgg_feats.memmap', dtype='float32', mode='r', shape=mem_shape) #===== prepare pickList ===== pickList = range(0, len(ID_PKL)) numToC = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'} answers = [] print "start making model..." #model = Keras_model_noLSTM.keras_model(1) model = Keras_model_deep_noLSTM.keras_model(1) model.load_weights(MODEL_NAME) #===== Start testing ===== print "Start testing!" for epoch in xrange(1): #NULL loop tStart = time.time() progress = ProgressBar().start() for batch_num in xrange(0, int(len(pickList) / BATCH_SIZE)): progress.update(int((batch_num + 1) / float(72801 / BATCH_SIZE) * 100)) start = batch_num * BATCH_SIZE end = (batch_num + 1) * BATCH_SIZE if end > len(pickList): start = len(pickList) - BATCH_SIZE end = len(pickList) batched_image = [] batched_words = []
import sys import Keras_model_deep_noLSTM as Akar import vqa (X_train,Y_train),(X_test,Y_test) = vqa.load_data() PREDICTION_FILE_NAME = '../predictions/test_elephat_test' MODEL_NAME = '../models/elephas/overfit_noLSTM_100.h5' TEST_ID = '../Data/pkl/img_q_id_test' TEST_ID_PKL = pickle.load(open(TEST_ID+'.pkl','rb')) print "start making model..." model = Akar.keras_model(1) model.load_weights(MODEL_NAME) print "Start testing..." prediction = model._predict([X_test]) def nameToId(ans_string): return '{0:{fill}{align}7}'.format(ans_string,fill='0',align='>') def numToC(ans_int): if ans_int == 0: return 'A' elif ans_int == 1: return 'B' elif ans_int == 2: return 'C'
#===== prepare pickList ===== pickList = range(0,len(ID_PKL)) random.shuffle(pickList) val_pickList = [] for idx in xrange(0,int(len(pickList)*VAL_RATIO)): val_pickList.append( pickList.pop(idx) ) # maybe this will help? -- Ray. # this really help, thanks! -- Angus. #print '{0:{fill}{align}12}'.format(ID_PKL[0][0],fill='0',align='>') # printing 300-dim word vector #print word_vec[ QUESTION_PKL[0][0] ] print "start making model..." #model = Keras_model_attention.keras_model(1) #model = Keras_model_noLSTM.keras_model(1) model = Keras_model_deep_noLSTM.keras_model(1) #===== Start training ===== csv_list = [] print "Start training!" for epoch in xrange(MAX_EPOCH): to_save = [] tStart = time.time() print "Epoch:",epoch+1 progress = ProgressBar().start() cost = 0 for batch_num in xrange(0,int(len(pickList)/BATCH_SIZE)): progress.update(int((batch_num+1)/float(146962*(1-VAL_RATIO)/BATCH_SIZE)*100)) start = batch_num*BATCH_SIZE end = (batch_num+1)*BATCH_SIZE if end > len(pickList):