Пример #1
0
def main(argv=None):
    prefetch_queue = Queue(maxsize=FLAGS.batch_size * 100)
    validation_queue = Queue(maxsize=FLAGS.batch_size * 10)

    for i in range(NUM_THREADS):
        prefetching_thread = PrefetchingThread(prefetch_queue,
                                               'PATH_TO_TRAIN_DIR')
        prefetching_thread.start()

    validation_prefetch_thread = PrefetchingThread(validation_queue,
                                                   'PATH_TO_VALIDATION_DIR')
    validation_prefetch_thread.start()

    training_phase = tf.placeholder_with_default(False, [],
                                                 name='training_phase')
    global_step = tf.Variable(0, name='global_step', trainable=False)

    num_steps_per_epoch = NUM_EXAMPLES // (FLAGS.batch_size * FLAGS.num_gpus)
    lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE,
                                    global_step,
                                    FLAGS.decay * num_steps_per_epoch,
                                    LEARNING_RATE_DECAY_FACTOR,
                                    staircase=True)
    tf.summary.scalar('learning_rate', lr, collections=['train'])

    opt = tf.train.AdamOptimizer(lr)
    # grads = optim.compute_gradients(loss, tf.trainable_variables())
    # apply_gradient_op = optim.apply_gradients(grads, global_step=global_step)

    tower_grads = []
    inputs = []
    y_s = []
    accuracys = []
    cross_entropies = []
    losses = []
    reuse_variables = None
    for i in range(FLAGS.num_gpus):
        with tf.device(assign_to_device('/gpu:{}'.format(i),
                                        ps_device='gpu:0')):
            with tf.name_scope('{}_{}'.format('TOWER', i)) as n_scope:
                with tf.name_scope('input'):
                    input = tf.placeholder(
                        tf.float32,
                        shape=[None, IMG_HEIGHT, IMG_WIDTH, IMG_CHAN],
                        name='img')
                    inputs.append(input)
                if FLAGS.resize != IMG_WIDTH:
                    input_resize = tf.image.resize_images(
                        input, [FLAGS.resize, FLAGS.resize])
                else:
                    input_resize = input
                input_resize = input_resize / 255.0
                input_resize = input_resize - 0.5
                input_resize = input_resize * 2.
                with tf.variable_scope(tf.get_variable_scope(),
                                       reuse=reuse_variables):
                    y_ = tf.placeholder(tf.int64, [None])
                    y_s.append(y_)
                    logits, net = xception(input_resize,
                                           training_phase,
                                           NUM_CLASSES,
                                           num_middle_blocks=8,
                                           reuse=reuse_variables)
                    tf.summary.histogram('{}/logits'.format(n_scope),
                                         logits,
                                         collections=['train'])
                    tower_loss = loss(logits, y_)
                    acc = accuracy(net, y_)
                    accuracys.append(acc)
                    losses.append(tower_loss)
                    cross_entropy = tf.losses.sparse_softmax_cross_entropy(
                        labels=y_, logits=logits)
                    cross_entropies.append(cross_entropy)
                    grads = opt.compute_gradients(tower_loss)
                    tower_grads.append(grads)
                reuse_variables = True

    acc = tf.reduce_mean(accuracys)
    loss_ = tf.reduce_mean(losses)
    cross_entropy_ = tf.reduce_mean(cross_entropies)
    tf.summary.scalar('loss/train', loss_, collections=['train'])
    tf.summary.scalar('loss/valid', cross_entropy_, collections=['validation'])
    tf.summary.scalar('accuracy/train', acc, collections=['train'])
    tf.summary.scalar('accuracy/valid', acc, collections=['validation'])

    # grads = average_gradients(tower_grads)
    # for grad, var in grads:
    #     tf.summary.histogram(var.name + '/grads', grad, collections=['train'])
    #     tf.summary.histogram(var.op.name + '/weights', var, collections=['train'])
    #
    # variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    # variables_averages_op = variable_averages.apply(tf.trainable_variables())

    # with tf.control_dependencies([apply_gradient_op, variables_averages_op]):
    #     train_op = tf.no_op(name='train')

    # Batch norm requires update_ops to be added as a train_op dependency.
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        train_op = opt.apply_gradients(grads, global_step=global_step)
    # train_op = apply_gradient_op

    train_summary_op = tf.summary.merge_all(key='train')
    valid_summary_op = tf.summary.merge_all(key='validation')

    if not tf.gfile.Exists(FLAGS.train_dir):
        tf.gfile.MakeDirs(FLAGS.train_dir)

    run_name = '{}'.format(datetime.now().strftime('%Y%m%d_%H%M%S'))
    run_dir = os.path.join(FLAGS.train_dir, run_name)

    if FLAGS.cont:
        run_dir = FLAGS.model_dir

    # Create a saver.
    saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)
    config = tf.ConfigProto(allow_soft_placement=True,
                            log_device_placement=FLAGS.log_device_placement)
    # config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        init = tf.global_variables_initializer()
        sess.run(init)

        if FLAGS.cont:
            load_saved_model(sess, saver)

        global_step_val = sess.run(global_step)

        if FLAGS.cont:
            print('Loaded model, step:', global_step_val)

        summary_writer = tf.summary.FileWriter(run_dir, sess.graph)
        for step in range(global_step_val, FLAGS.max_steps):
            dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            feed_dict = {}
            t0 = time.time()
            for i in range(FLAGS.num_gpus):
                batch_imgs = []
                batch_labels = []
                for _ in range(FLAGS.batch_size):
                    (_, _, cls), img = prefetch_queue.get()
                    batch_imgs.append(img)
                    batch_labels.append(cls)
                feed_dict[inputs[i]] = batch_imgs
                feed_dict[y_s[i]] = batch_labels
                feed_dict[training_phase] = True
            fetch_time = time.time() - t0
            t1 = time.time()
            _, loss_val, acc_val = sess.run([train_op, loss_, acc],
                                            feed_dict=feed_dict)
            duration = time.time() - t1

            if step % 10 == 0:
                summary_str = sess.run(train_summary_op, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, global_step=step)
                num_examples_per_step = FLAGS.batch_size
                examples_per_sec = (num_examples_per_step /
                                    duration) * FLAGS.num_gpus
                sec_per_batch = duration

                format_str = '{}: step {} loss = {:.8f} acc = {:.4f} ({:.2f} ex/s; {:.2f} s/batch; fetch: {:.2f})'
                print(
                    format_str.format(dt, step, loss_val, acc_val,
                                      examples_per_sec, sec_per_batch,
                                      fetch_time))

            if step % 100 == 0:
                feed_dict = {}
                t0 = time.time()
                for i in range(FLAGS.num_gpus):
                    batch_imgs = []
                    batch_labels = []
                    for _ in range(FLAGS.batch_size):
                        (_, _, cls), img = validation_queue.get()
                        batch_imgs.append(img)
                        batch_labels.append(cls)
                    feed_dict[inputs[i]] = batch_imgs
                    feed_dict[y_s[i]] = batch_labels
                    feed_dict[training_phase] = True
                fetch_time = time.time() - t0
                t1 = time.time()
                summary_str, val_loss, val_acc = sess.run(
                    [valid_summary_op, cross_entropy_, acc],
                    feed_dict=feed_dict)
                duration = time.time() - t1
                summary_writer.add_summary(summary_str, global_step=step)
                format_str = '{}: (VALID) step {} loss = {:.8f} acc = {:.4f} time: {:.2f} fetch: {:.2f}'
                print(
                    format_str.format(dt, step, val_loss, val_acc, duration,
                                      fetch_time))

            # Save the model checkpoint periodically.
            if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_path = os.path.join(run_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=global_step)
Пример #2
0
])

trainset = torchvision.datasets.CIFAR100(root='./data',
                                         train=True,
                                         download=True,
                                         transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset,
                                          batch_size=args.bs,
                                          shuffle=True,
                                          num_workers=2)

# testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
# testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)

if args.model == 'xception':
    net = xception()
net.to(device)

# loss_function = nn.CrossEntropyLoss()
# optimizer = optim.SGD(net.parameters(), lr=args.base_lr, momentum=0.9, weight_decay=1e-4, nesterov=True)

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(),
                      lr=args.base_lr,
                      momentum=0.9,
                      weight_decay=5e-4,
                      nesterov=True)

# lr_finder = LRFinder(net, optimizer, criterion, device=device)
# lr_finder.range_test(trainloader, end_lr=1, num_iter=200)
# lr_finder.plot(log_lr=False)
    p = p[0][:50]
    p = p[np.newaxis, :]
    return p


if __name__ == "__main__":
    restart = False
    rootpath = r""
    csvpath = r""  #csv\seperation.csv
    datapath = r""  #overlapping_separation_audio_generator\two_birds\audios
    random.seed(7)
    torch.manual_seed(7)
    torch.cuda.manual_seed(7)
    torch.cuda.manual_seed_all(7)
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model = xception()
    print("model done!")
    load_model = torch.load(os.path.join(rootpath, "save/best_Xecption.pt"),
                            map_location=torch.device('cpu'))
    model.load_state_dict(load_model["model_state_dict"])
    print("load done")
    model.to(device)
    model.eval()
    CLASS = subclass.CLASS
    if restart == False:
        pre = []
        labels = []
        save = {"pre": [], "labels": [], "lastindex": -1}
        lastindex = -1
    else:
        with open(r"data/seperationtemp.json", 'r') as f:
Пример #4
0
def main(argv=None):
    with tf.name_scope('input'):
        input = tf.placeholder(tf.float32, shape=[None, IMG_HEIGHT, IMG_WIDTH, IMG_CHAN], name='img')
        if FLAGS.resize != IMG_WIDTH:
            input_resize = tf.image.resize_images(input, [FLAGS.resize, FLAGS.resize])
        else:
            input_resize = input
        input_resize = input_resize / 255.0
        input_resize = input_resize - 0.5
        input_resize = input_resize * 2.
        training_phase = tf.placeholder_with_default(False, [], name='training_phase')

        _, net = xception(input_resize, num_classes=5270, training_phase=training_phase)

    net_cls = tf.argmax(net, axis=1)

    # Create a saver.
    saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)

    processed = set()
    # with open('./submission.csv_bak', 'r') as f:
    #     i = 0
    #     for line in f:
    #         if i < 1:
    #             i += 1
    #         else:
    #             prod_id = int(line.split(',')[0])
    #             processed.add(prod_id)

    with open(FLAGS.out_file, 'w') as out_file:
        out_file.write('_id,category_id\n')
        config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=FLAGS.log_device_placement)
        with tf.Session(config=config) as sess:
            init = tf.global_variables_initializer()
            sess.run(init)

            load_saved_model(sess, saver)

            data = bson.decode_file_iter(open('PATH_TO_BSON_TEST_SET', 'rb'))

            with open('FILE_WITH_CATEGORIES_DICT', 'r') as f:
                label_to_cat = json.load(f)
            int_label_to_cat = {int(k): v for k, v in label_to_cat.items()}

            batch_imgs = []
            batch_prods = []
            step = 0
            for i, d in enumerate(data):
                prod_id = int(d['_id'])
                if prod_id in processed:
                    continue
                for e, pic in enumerate(d['imgs']):
                    pic = imread(io.BytesIO(pic['picture']))
                    batch_imgs.append(np.array(pic))
                    batch_prods.append(prod_id)

                if len(batch_imgs) >= FLAGS.batch_size:
                    feed_dict = {
                        input: batch_imgs,
                        training_phase: False
                    }

                    dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    t0 = time.time()
                    probs, predictions = sess.run([net, net_cls], feed_dict=feed_dict)
                    duration = time.time() - t0

                    labels = predictions
                    prod_to_labels = {}
                    for prod, label in zip(batch_prods, labels):
                        prod = int(prod)
                        label = int(label)
                        if prod not in prod_to_labels:
                            prod_to_labels[prod] = []
                        prod_to_labels[prod].append(label)

                    uniq_prods = set(prod_to_labels.keys())
                    sorted_prods = sorted(uniq_prods)

                    for p in sorted_prods:
                        label = stats.mode(prod_to_labels[p])[0][0]
                        cls = int_label_to_cat[label]
                        out_file.write('{},{}\n'.format(p, cls))
                        step += 1

                    format_str = '{}: prod {} / {} | dur: {:.2f}'
                    print(format_str.format(dt, step, NUM_EXAMPLES, duration))
                    batch_imgs = []
                    batch_prods = []

            feed_dict = {
                input: batch_imgs,
                training_phase: False
            }

            dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            t0 = time.time()
            predictions = sess.run(net_cls, feed_dict=feed_dict)
            step += 1
            duration = time.time() - t0

            labels = predictions
            prod_to_labels = {}
            for prod, label in zip(batch_prods, labels):
                prod = int(prod)
                label = int(label)
                if prod not in prod_to_labels:
                    prod_to_labels[prod] = []
                prod_to_labels[prod].append(label)

            uniq_prods = set([int(x) for x in batch_prods])
            uniq_prods = sorted(uniq_prods)

            for p in uniq_prods:
                label = stats.mode(prod_to_labels[p])[0][0]
                cls = int_label_to_cat[label]
                out_file.write('{},{}\n'.format(p, cls))
                step += 1

            format_str = '{}: prod {} / {} | dur: {:.2f}'
            print(format_str.format(dt, step, NUM_EXAMPLES, duration))