Exemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser(description='extract features')
    parser.add_argument('--gpu', '-g', default=0, type=int, help='GPU ID')
    parser.add_argument('--ALL_DATA_PATH', '-all', default='../data/processed_data.pickle')
    parser.add_argument('--MODEL_PATH', '-model', default='../data/resnet_152.caffemodel')
    parser.add_argument('--SAVE_PATH', '-save', default='../data/all_feature.pickle')
    args = parser.parse_args()

    gpu_device = args.gpu
    all_data_path = args.ALL_DATA_PATH
    model_path = args.MODEL_PATH
    save_path = args.SAVE_PATH

    with open(all_data_path, 'rb') as f:
        data = pickle.load(f)

    cuda.get_device_from_id(gpu_device).use()
    model = ResNet(model_path, 152)
    model.to_gpu(gpu_device)

    all_dic = {}

    for each_question in data:
        try:
            image_path = each_question['image_path']
            image = io.imread(image_path)
            if image.ndim == 2:
                image = np.tile(image[:, :, np.newaxis], (1, 1, 3))

            qa_id = each_question['qa_id']
            crop_region = [each_question['x'], each_question['y'], each_question['w'], each_question['h']]
            h, w, _ = image.shape
            x_region, y_region, w_region, h_region = crop_region

            resize_entire = image_resize(image)
            resize_region = region_resize(image, crop_region)

            entire_feature = feature_extract(resize_entire, model)
            region_feature = feature_extract(resize_region, model)
            concat_feature = np.concatenate((region_feature, entire_feature), axis=1)[0]

            x_tl, y_tl, x_br, y_br = x_region, y_region, x_region + w_region, y_region + h_region
            region_inf = np.asarray([x_tl/w, y_tl/h, x_br/w, y_br/h, (w_region * h_region)/(w * h)])
            concat_all = np.concatenate((concat_feature, region_inf), axis=0)

            all_dic[qa_id] = concat_all
        except:
            continue

    with open(save_path, 'wb') as f:
        pickle.dump(all_dic, f)
Exemplo n.º 2
0
#gpu_device=0
#cuda.get_device(gpu_device).use()
#model.to_gpu(gpu_device)

tr_im = loadim(a=-1, j="trainimages")
tr_ma = loadim(a=-1, j="trainmasks")
#teimage=loadim(a=-1,j="test")

#trdepth,tedepth=loadcsv()
#trdepth=sorted(trdepth,key=lambda t:t[0])
#tedepth=sorted(tedepth,key=lambda t:t[0])
#tr_dep=[int(t[1]) for t in trdepth]
#te_dep=[int(t[1]) for t in tedepth]
#tr_im_ma_dep=[[tr_im[i],tr_ma[i],tr_dep[i]] for i in range(4000)]
#tr_im_ma_dep=sorted(tr_im_ma_dep,key=lambda t:t[2])

gpu_device = 0
X = cuda.to_gpu(tr_im, device=0)
Y = cuda.to_gpu(tr_ma, device=0)

model = ResNet()
cuda.get_device(gpu_device).use()
model.to_gpu(gpu_device)

trainer = Trainer(model)

trainer.fit(X)

df_loss = pd.DataFrame(trainer.loss)
df_loss.to_csv('loss_csv')