Ejemplo n.º 1
0
    end = (n_batch + 1) * N_batch
    cont_sentences = np.zeros([T, N_batch], dtype=np.float32)
    input_sentences = np.zeros([T, N_batch], dtype=np.float32)
    target_sentences = np.zeros([T, N_batch], dtype=np.float32)
    bbox_coordinates = np.zeros([N_batch, 8], dtype=np.float32)
    fc7_context = np.zeros([N_batch, 4096], dtype=np.float32)
    fc7_local = np.zeros([N_batch, 4096], dtype=np.float32)
    query = []
    for n_pair in range(begin, end):
        # Append 0 as dummy label
        imcrop_path = resized_imcrop_dir + train_pairs[n_pair][0] + '.png 0'
        imcrop_list.append(imcrop_path)
        # Append 0 as dummy label
        wholeim_path = image_dir + train_pairs[n_pair][3] + '.jpg 0'
        wholeim_list.append(wholeim_path)
        stream = retriever.sentence2vocab_indices(train_pairs[n_pair][1],
                                                  vocab_dict)
	query.append(train_pairs[n_pair][1])
        if len(stream) > T-1:
            stream = stream[:T-1]
        pad = T - 1 - len(stream)
        cont_sentences[:, n_pair-begin] = [0] + [1] * len(stream) + [0] * pad
        input_sentences[:, n_pair-begin] = [0] + stream + [-1] * pad
        target_sentences[:, n_pair-begin] = stream + [0] + [-1] * pad
        bbox_coordinates[n_pair-begin, :] = np.squeeze(train_pairs[n_pair][2])
        fc7_context[n_pair-begin, :] = train_pairs[n_pair][4]
        fc7_local[n_pair-begin, :] = train_pairs[n_pair][5]
	
    h5_training_filename = './data/training/50_training_data/' + 'training_%d_to_%d' % (begin, end)
    np.savez(h5_training_filename, raw_query=query, bbox_coordinates = bbox_coordinates, fc7_context = fc7_context, fc7_local = fc7_local)

# generate hdf5 files
if not os.path.isdir(save_hdf5_dir):
    os.mkdir(save_hdf5_dir)
for n_batch in range(num_batch):
    if (n_batch+1) % 10 == 0:
        print('writing batch %d / %d' % (n_batch+1, num_batch))
    begin = n_batch * N_batch
    end = (n_batch + 1) * N_batch
    cont_sentences = np.zeros([T, N_batch], dtype=np.float32)
    input_sentences = np.zeros([T, N_batch], dtype=np.float32)
    target_sentences = np.zeros([T, N_batch], dtype=np.float32)
    for n_pair in range(begin, end):
        # Append 0 as dummy label
        image_path = image_dir + train_pairs[n_pair][0] + '.JPEG 0' # 0 as dummy label
        image_list.append(image_path)

        stream = retriever.sentence2vocab_indices(train_pairs[n_pair][1], vocab_dict)
        if len(stream) > T-1:
            stream = stream[:T-1]
        pad = T - 1 - len(stream)
        cont_sentences[:, n_pair-begin] = [0] + [1] * len(stream) + [0] * pad
        input_sentences[:, n_pair-begin] = [0] + stream + [-1] * pad
        target_sentences[:, n_pair-begin] = stream + [0] + [-1] * pad
    h5_filename = save_hdf5_dir + '%d_to_%d.h5' % (begin, end)
    retriever.write_batch_to_hdf5(h5_filename, cont_sentences, input_sentences, target_sentences)
    hdf5_list.append(h5_filename)

util.io.save_str_list(image_list, save_image_list_file)
util.io.save_str_list(hdf5_list, save_hdf5_list_file)