Пример #1
0
def dump_result(name, ckpt, image_pack_name=None):
    if image_pack_name is None:
        data = None
    else:
        data = load_data(image_pack_name.split(','))
    outputs = []
    gts = []
    with get_session() as sess:
        fcn = FCN(sess=sess, name=name)
        fcn.load(ckpt)
        _, _, outputs, gts = fcn.test(scales=[0.5],
                                      summary=True,
                                      summary_key=123,
                                      data=data)
    result = {
        'outputs': np.array(outputs),
        'gts': np.array(gts),
    }
    pickle.dump(
        result,
        open("outputs/%s-%s-%s.pkl" % (name, ckpt, image_pack_name), "wb"))
Пример #2
0
def eva_model(testfile, nb_class, model_path, model_name, result_path,
              all_res_file):
    x, y_true, w2vmat = load_data(testfile,
                                  maxlen,
                                  nb_class,
                                  False,
                                  w2v,
                                  i2v,
                                  w2vdim,
                                  w2idic,
                                  train=False)

    if not os.path.isdir(result_path):
        os.mkdir(result_path)

    ouf = open(result_path + '/' + model_name + '_score.txt', 'w')
    model = load_model(model_path + '/' + model_name + '.model')
    print('Model successfully loaded: {}'.format(model_name))

    all_res_file = open(all_res_file, 'a')
    all_res_file.write(model_name + '\n')

    y_true = np.argmax(y_true, axis=1)
    y_pred = np.argmax(model.predict(x), axis=1)

    res = {}
    res['Acc'] = round(accuracy_score(y_true=y_true, y_pred=y_pred), 3)
    res['Precision'] = round(precision_score(y_true=y_true, y_pred=y_pred), 3)
    res['Recall'] = round(recall_score(y_true=y_true, y_pred=y_pred), 3)
    res['F-score'] = round(f1_score(y_true=y_true, y_pred=y_pred), 3)

    print(res)

    for key in res:
        string = '{}: {}\t'.format(key, res[key])
        ouf.write(string)
        all_res_file.write(string)

    ouf.write('\n')
    all_res_file.write('\n\n')
Пример #3
0
    def test_network(self, summary=False, summary_key=0):
        records = load_data(['m'])

        for r in records:
            scale = 1
            a = 1
            img = np.clip((r.img / r.img.max()), 0, 1)
            img = np.power(img, 2.2)
            img[:, :img.shape[1] // 2:, 0] *= 3
            #img = np.pad(img, ((112, 112), (112, 112), (0, 0)), 'constant')
            if scale != 1.0:
                img = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            shape = img.shape[:2]
            if shape not in self.test_nets:
                test_net = {}
                test_net['images'] = tf.placeholder(tf.float32,
                                                    shape=(None, shape[0],
                                                           shape[1], 3),
                                                    name='test_images')
                with tf.variable_scope("FCN", reuse=True):
                    test_net['pixels'] = FCN.build_branches(
                        test_net['images'], 1.0)
                self.test_nets[shape] = test_net
            test_net = self.test_nets[shape]

            pixels = self.sess.run(test_net['pixels'],
                                   feed_dict={
                                       test_net['images']: img[None, :, :, :],
                                   })

            pixels = pixels[0].astype(np.float32)
            #pixels = cv2.resize(pixels, image.shape[0:2][::-1])
            pixels /= np.linalg.norm(pixels, axis=2)[:, :, None]

            #pixels = pixels[:,:,::-1]

            cv2.imshow('pixels', cv2.resize(pixels, (0, 0), fx=10, fy=10))
            cv2.imshow('image', cv2.resize(img, (0, 0), fx=0.5, fy=0.5))
            cv2.waitKey(0)
Пример #4
0
def test_input_gamma(name,
                     ckpt,
                     input_gamma,
                     image_pack_name=None,
                     output_filename=None):
    config_set_input_gamma(float(input_gamma))
    if image_pack_name is None:
        data = None
    else:
        data = load_data(image_pack_name.split(','))
    with get_session() as sess:
        fcn = FCN(sess=sess, name=name)
        fcn.load(ckpt)
        _, _, _, _, ret = fcn.test(scales=[0.5],
                                   summary=True,
                                   summary_key=123,
                                   data=data)
        if output_filename is not None:
            with open('outputs/%s.pkl' % output_filename, 'wb') as f:
                pickle.dump(ret, f)
            print ret
            print 'results dumped'
Пример #5
0
    test_corr = numpy.mean(test_correlations)
    test_mean = numpy.mean(test_mean)
    running_time = time.time() - start_time

    logging.info('test correlation {0:.2f} (mean {1:.2f}), time {2:.2f}s' \
        .format(-test_corr, test_mean, running_time))


if __name__ == "__main__":

    logging.basicConfig(format='%(asctime)-15s %(levelname)s: %(message)s',
                        datefmt='%m/%d/%Y %H:%M:%S',
                        level=logging.INFO)

    # load dataset
    dataset = './mnist.pkl.gz'
    datasets = load_data(dataset, shared=True)

    # set configuration
    cfg = Config()
    cfg.set_data_config(datasets)

    # build model
    model = ParallelDNN(cfg, datasets)

    # train
    train(cfg, model)
    test(cfg, model)

# THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32,force_device=True python ./deep_cca.py
Пример #6
0
    def test(self,
             summary=False,
             scales=[1.0],
             weights=[],
             summary_key=0,
             data=None,
             eval_speed=False,
             visualize=False):
        if not TEST_FOLDS:
            return [0]
        if data is None:
            records = load_data(TEST_FOLDS)
        else:
            records = data
        avg_errors = []
        median_errors = []
        t = time.time()

        summaries = []
        if weights == []:
            weights = [1.0] * len(scales)

        outputs = []
        ground_truth = []
        avg_confidence = []

        errors = []
        for r in records:
            all_pixels = []
            for scale, weight in zip(scales, weights):
                img = r.img
                if scale != 1.0:
                    img = cv2.resize(img, (0, 0), fx=scale, fy=scale)
                shape = img.shape[:2]
                if shape not in self.test_nets:
                    aspect_ratio = 1.0 * shape[1] / shape[0]
                    if aspect_ratio < 1:
                        target_shape = (MERGED_IMAGE_SIZE,
                                        MERGED_IMAGE_SIZE * aspect_ratio)
                    else:
                        target_shape = (MERGED_IMAGE_SIZE / aspect_ratio,
                                        MERGED_IMAGE_SIZE)
                    target_shape = tuple(map(int, target_shape))

                    test_net = {}
                    test_net['illums'] = tf.placeholder(tf.float32,
                                                        shape=(None, 3),
                                                        name='test_illums')
                    test_net['images'] = tf.placeholder(tf.float32,
                                                        shape=(None, shape[0],
                                                               shape[1], 3),
                                                        name='test_images')
                    with tf.variable_scope("FCN", reuse=True):
                        test_net['pixels'] = FCN.build_branches(
                            test_net['images'], 1.0)
                        test_net['est'] = tf.reduce_sum(test_net['pixels'],
                                                        axis=(1, 2))
                    test_net['merged'] = get_visualization(
                        test_net['images'], test_net['pixels'],
                        test_net['est'], test_net['illums'], target_shape)
                    self.test_nets[shape] = test_net
                test_net = self.test_nets[shape]

                pixels, est, merged = self.sess.run(
                    [test_net['pixels'], test_net['est'], test_net['merged']],
                    feed_dict={
                        test_net['images']: img[None, :, :, :],
                        test_net['illums']: r.illum[None, :]
                    })

                if eval_speed:
                    eval_batch_size = 1
                    eval_packed_input = img[None, :, :, :].copy()
                    eval_packed_input = np.concatenate(
                        [eval_packed_input for i in range(eval_batch_size)],
                        axis=0)
                    eval_packed_input = np.ascontiguousarray(eval_packed_input)
                    eval_start_t = time.time()
                    print(eval_packed_input.shape)
                    eval_rounds = 100
                    images_variable = tf.Variable(
                        tf.random_normal(eval_packed_input.shape,
                                         dtype=tf.float32,
                                         stddev=1e-1))
                    print(images_variable)
                    for eval_t in range(eval_rounds):
                        print(eval_t)
                        pixels, est = self.sess.run(
                            [test_net['pixels'], test_net['est']],
                            feed_dict={
                                test_net['images']:  #images_variable,
                                eval_packed_input,
                            })
                    eval_elapsed_t = time.time() - eval_start_t
                    print('per image evaluation time',
                          eval_elapsed_t / (eval_rounds * eval_batch_size))

                pixels = pixels[0]
                #est = est[0]
                merged = merged[0]

                all_pixels.append(weight * pixels.reshape(-1, 3))

            all_pixels = np.sum(np.concatenate(all_pixels, axis=0), axis=0)
            est = all_pixels / (np.linalg.norm(all_pixels) + 1e-7)
            outputs.append(est)
            ground_truth.append(r.illum)
            error = math.degrees(angular_error(est, r.illum))
            errors.append(error)
            avg_confidence.append(np.mean(np.linalg.norm(all_pixels)))

            summaries.append((r.fn, error, merged))
        print("Full Image:")
        ret = print_angular_errors(errors)
        ppt = (time.time() - t) / len(records)
        print('Test time:', time.time() - t, 'per image:', ppt)

        if summary:
            for fn, error, merged in summaries:
                folder = self.get_ckpt_folder() + '/test%04dsummaries_%4f/' % (
                    summary_key, scale)
                try:
                    os.mkdir(folder)
                except:
                    pass
                summary_fn = '%s/%5.3f-%s.png' % (folder, error, fn)
                cv2.imwrite(summary_fn, merged[:, :, ::-1] * 255)

        if visualize:
            for fn, error, merged in summaries:
                cv2.imshow('Testing', merged[:, :, ::-1])
                cv2.waitKey(0)

        return errors, ppt, outputs, ground_truth, ret, avg_confidence
Пример #7
0
    args.input_dir = os.path.normpath(args.input_dir)
    len_input_dir = len(args.input_dir)
    assert os.path.exists(args.input_dir), "Input directory not found"

    input_glob = os.path.join(args.input_dir, args.input_pattern)
    files = pc_io.get_files(input_glob)
    assert len(files) > 0, "No input files found"
    filenames = [x[len_input_dir + 1:] for x in files]
    output_files = [
        os.path.join(args.output_dir, x + '.bin') for x in filenames
    ]
    output_infs = [
        os.path.join(args.output_dir, x + '.inf.bin') for x in filenames
    ]
    pcQueue_512, pcQueue_1024, pcQueue_2048 = data_provider.load_data(
        args.input_dir, args.source_extension)

    centroid_list = []
    furthest_distance_list = []
    meta_matrix, nor_pc_512, nor_pc_1024, nor_pc_2048 = data_provider.gen_meta(
        centroid_list, furthest_distance_list, pcQueue_512, pcQueue_1024,
        pcQueue_2048)
    compressed_bytes = fpzip.compress(meta_matrix.astype(np.float32),
                                      precision=0,
                                      order='C')
    with open(output_infs[0], 'wb') as f:
        f.write(compressed_bytes)
    f.close()

    def patch_compress(points, model, checkpoint):
        estimator = tf.estimator.Estimator(model_fn=model.model_fn,
Пример #8
0
    #clear dir
    shutil.rmtree(directory, ignore_errors=True)
    #create dir
    try:
        os.mkdir(directory)
    except OSError as e:
        print("Couldn't create directory {0}, {1}".format(directory, e))

    #save ouputs in new dir
    i = 0
    for m in mix:
        i += 1

        file_name = ("{0:02d}. {1}.mp3").format(i, m[0])
        song_path = os.path.join(directory, file_name)

        song = m[1]

        #, tags={'artist': 'Various artists', 'album': 'Best of 2011', 'comments': 'This album is awesome!'})
        song.export(song_path, format="mp3", tags={'album': set_name, '#': i})
        print(file_name)

ds = data_provider.load_data('/home/wojtek/VBoxShared/Mixes')

for data in ds:
    mix = split_audio(*data[:2])
    set_name = data[2]
    root = data[3]

    save_outputs(root, set_name, mix)
Пример #9
0

if __name__ == '__main__':
    parser = setup_arguments()
    args = parser.parse_args()
    # model_info = describe_arguments(args)

    from data_provider import filter_by_user_info, get_solo_only, \
    get_data_last_only, map_user_tasks, extract_data, calc_velocity, extract_simple_features, extract_data_distance, \
        load_data
    from training import train_model, train_simple_model

    np.random.seed(args.seed)
    random.seed(args.seed)

    user_info, data_nc_with_km_sr = load_data()

    users_tasks, _ = map_user_tasks(data_nc_with_km_sr, user_info)

    pre_train_time = time.time()
    if args.only_solo:
        data_nc_with_km_sr_solo = get_solo_only(data_nc_with_km_sr)
        data_nc_with_km_sr_solo_filtered = filter_by_user_info(
            user_info, data_nc_with_km_sr_solo, users_tasks)
        dfs = data_nc_with_km_sr_solo_filtered
        train_data_km_solo = extract_data(data_nc_with_km_sr_solo_filtered,
                                          users_tasks, user_info,
                                          args.max_events, args.min_events,
                                          args.standardize, args.normalize,
                                          args.reset_origin, args.norm_time)
        data = train_data_km_solo
Пример #10
0
def eva_model(train_split_file, nb_class, model_path, model_name, result_path,
              rel_dic):
    x, x_w, x_jcd, x_med, y, c2vmat, w2vmat = load_data(train_split_file,
                                                        maxlen,
                                                        nb_class,
                                                        rel_dic,
                                                        mode,
                                                        input_num,
                                                        False,
                                                        w2v,
                                                        ci2v,
                                                        wi2v,
                                                        w2vdim,
                                                        w2idic,
                                                        c2idic,
                                                        info,
                                                        train=True)

    model = load_model(model_path + '/' + model_name + '.model')

    print('Model successfully loaded: {}'.format(model_name))

    time_start = time.time()
    if mode == 'charword':
        res = model.evaluate([x[0], x_w[0]], y, batch_size=batch_size)
    else:
        res = model.evaluate(x, y, batch_size=batch_size)
    time_end = time.time()

    print('Average time cost per sample: ',
          1000 * (time_end - time_start) / len(y), 'ms')
    print('Loss and acc: ', res)

    if mode == 'charword':
        pred = model.predict([x[0], x_w[0]])
    else:
        pred = model.predict(x)

    y_true = np.argmax(y, axis=1)
    y_pred = np.argmax(pred, axis=1)
    f_score = round(f1_score(y_true=y_true, y_pred=y_pred, average='macro'), 4)
    print('fscore:', f_score)

    n = 0
    num_top1, num_top3, num_topN = 0, 0, 0
    err_top1, err_top3, err_topN = [], [], []
    real_lab = defaultdict(int)
    err_mat = defaultdict(int)

    rel_dic_rev = dict(zip(rel_dic.values(), rel_dic.keys()))

    if not os.path.isdir(result_path):
        os.mkdir(result_path)

    with open(train_split_file, 'r') as inf, open(result_path + '/' + model_name + '_score.txt', 'w') as ouf1,\
      open(result_path + '/' + model_name + '_matrix.txt', 'w') as ouf2:
        ouf1.write('\t'.join(
            ['Sentence', 'True_label', 'Pred_label', 'Score', 'Correct']) +
                   '\n')
        for line in inf:
            line = line.strip('\n').split('\t')[1:]
            q, l, pr = line[0], rel_dic[line[1]], pred[n]
            n += 1
            real_lab[line[1]] += 1

            if l != np.argmax(pr):
                cor = 0
            else:
                cor = 1
            pr_dic = dict(zip([rel_dic_rev[i] for i in range(len(pr))], pr))
            pr_top = sorted(pr_dic.items(),
                            key=operator.itemgetter(1),
                            reverse=True)[0]
            ouf1.write(
                '\t'.join([q, line[1], pr_top[0],
                           str(pr_top[1]),
                           str(cor)]) + '\n')
            if l != np.argmax(pr):
                err_top1.append(line[1])
                err_mat[(line[1], str(pr_top[0]))] += 1

        err_mat = sorted(err_mat.items(),
                         key=operator.itemgetter(1),
                         reverse=True)
        ouf2.write(
            '\t'.join(['True_label', 'Pred_label', 'Err_count', 'Err_ratio']) +
            '\n')
        for pair, count in err_mat:
            ouf2.write('\t'.join([
                pair[0], pair[1],
                str(count),
                str(count / Counter(err_top1)[pair[0]])
            ]) + '\n')

        err_top1 = sorted(Counter(err_top1).items(),
                          key=operator.itemgetter(1),
                          reverse=True)
        ouf2.write(
            '\n' +
            '\t'.join(['True_label', 'Err_count', 'All_count', 'Accuracy']) +
            '\n')
        for rela, count in err_top1:
            ouf2.write('\t'.join([
                rela,
                str(count),
                str(real_lab[rela]),
                str(1 - count / real_lab[rela])
            ]) + '\n')

        ouf2.write('\n' + 'Overall Accuracy:' + '\t' + str(res[1]))
Пример #11
0
def train_model(nb_class, trainfile, valfile, model_path, model_name, epoch):

    x, y, w2vmat = load_data(trainfile,
                             maxlen,
                             nb_class,
                             shuffle,
                             w2v,
                             i2v,
                             w2vdim,
                             w2idic,
                             train=True)

    if valfile:
        x_v, y_v, w2vmat_v = load_data(valfile,
                                       maxlen,
                                       nb_class,
                                       shuffle,
                                       w2v,
                                       i2v,
                                       w2vdim,
                                       w2idic,
                                       train=True)
        val = [x_v, y_v]
    else:
        val = None

    print('Building base model: {}'.format(model_name))

    model = model_structure.create_model(model_name, model_type, nb_class,
                                         nb_filter, kernel_size, vocab_size,
                                         maxlen, denseuni, dropout, l2, lr,
                                         w2v, w2vdim, w2vmat)
    plot_model(model,
               show_shapes=True,
               to_file='model_diagram/' + model_name + '.png')

    if not os.path.isdir(model_path):
        os.mkdir(model_path)
    checkpoint = ModelCheckpoint(model_path + '/' + model_name + '.model',
                                 monitor=monitor,
                                 verbose=1,
                                 save_weights_only=False,
                                 save_best_only=True,
                                 mode='auto')

    earlystop = EarlyStopping(monitor=monitor,
                              patience=patience,
                              verbose=1,
                              mode='auto')

    print('Begin training model: {}'.format(model_name))

    history = model.fit(x,
                        y,
                        validation_data=val,
                        batch_size=batch_size,
                        epochs=epoch,
                        callbacks=[checkpoint, earlystop],
                        validation_split=split,
                        class_weight=get_class_weights(y, smooth_factor),
                        verbose=2)

    print('Model successfully trained: {}'.format(model_name))

    return model, history
Пример #12
0
def train_model(gpu, input_num, nb_class, rel_dic, trainfile, valfile,
                model_path, model_name, epoch):

    x, x_w, x_jcd, x_med, y, c2vmat, w2vmat = load_data(trainfile,
                                                        maxlen,
                                                        nb_class,
                                                        rel_dic,
                                                        mode,
                                                        input_num,
                                                        shuffle,
                                                        w2v,
                                                        ci2v,
                                                        wi2v,
                                                        w2vdim,
                                                        w2idic,
                                                        c2idic,
                                                        info,
                                                        train=True)
    if valfile:
        x_v, x_w_v, x_jcd_v, x_med_v, y_v, c2vmat_v, w2vmat_v = load_data(
            valfile,
            maxlen,
            nb_class,
            rel_dic,
            mode,
            input_num,
            shuffle,
            w2v,
            ci2v,
            wi2v,
            w2vdim,
            w2idic,
            c2idic,
            info,
            train=True)
        val = [x_v, y_v]

    else:
        val = None

    print('Building base model: {}'.format(model_name))

    model = multi_cgru_keras.creat_model(gpu, model_name, mode, nb_class,
                                         nb_filter, wsize, maxlen, cvsize,
                                         wvsize, denseuni, cnn_dropout, l2, lr,
                                         w2v, w2vdim, c2vmat, w2vmat)
    plot_model(model, show_shapes=True, to_file='model_cgru_cw.png')

    if not os.path.isdir(model_path):
        os.mkdir(model_path)

    checkpoint = ModelCheckpoint(model_path + '/' + model_name + '.model',
                                 monitor=monitor,
                                 verbose=1,
                                 save_weights_only=False,
                                 save_best_only=True,
                                 mode='auto')

    earlystop = EarlyStopping(monitor=monitor,
                              patience=patience,
                              verbose=1,
                              mode='auto')

    print('Begin training model: {}'.format(model_name))

    if mode == 'charword':
        history = model.fit([x[0], x_w[0]],
                            y,
                            validation_data=val,
                            batch_size=batch_size,
                            epochs=epoch,
                            callbacks=[checkpoint, earlystop],
                            validation_split=split,
                            class_weight=get_class_weights(y, smooth_factor))
    else:
        print(x[0][0])

        history = model.fit(x,
                            y,
                            validation_data=val,
                            batch_size=batch_size,
                            epochs=epoch,
                            callbacks=[checkpoint, earlystop],
                            validation_split=split,
                            class_weight=get_class_weights(y, smooth_factor))

    print('Model successfully trained: {}'.format(model_name))

    return model, history