def load_dataset(): # We first define a download function, supporting both Python 2 and 3. if sys.version_info[0] == 2: from urllib import urlretrieve else: from urllib.request import urlretrieve def check_download(filename, source='http://www.aaalgo.com/picpac/datasets/cifar/'): if not os.path.exists(filename): print("Downloading %s" % filename) urlretrieve(source + filename, filename) config = dict(seed=1996, resize_width=32, resize_height=32, batch=batch_size, channels=3, channel_first=False, onehot=nb_classes) train_config = dict( shuffle=True, reshuffle=True, split=10, split_fold=1, stratify=True, pert_color1=10, pert_color2=10, pert_color3=10, pert_angle=15, pert_min_scale=0.7, pert_max_scale=1.2, ) train_config.update(config) # We can now download and read the training and test set images and labels. check_download('cifar10-train.picpac') check_download('cifar10-test.picpac') train = picpac.ImageStream('cifar10-train.picpac', loop=True, split_negate=False, perturb=data_augmentation, **train_config) val = picpac.ImageStream('cifar10-train.picpac', loop=False, split_negate=True, perturb=False, **train_config) test = picpac.ImageStream('cifar10-test.picpac', loop=False, perturb=False, **config) return train, val, test
def create_picpac_stream (path, is_training): assert os.path.exists(path) config = {"db": path, "loop": is_training, "shuffle": is_training, "reshuffle": is_training, "annotate": [1], "channels": 3, "stratify": is_training, "dtype": "float32", "batch": BATCH, "colorspace": 'BGR', "cache": 0, "order": "NCHW", # specific to caffe "transforms": [ {"type": "resize", "width": WIDTH, "height": HEIGHT}, {"type": "augment.flip", "horizontal": True, "vertical": False, "transpose": False}, {"type": "augment.scale", "min":0.8, "max":1.2}, {"type": "augment.rotate", "range": 10}, {"type": "augment.add", "range":20}, {"type": "clip", "width": WIDTH, "height": HEIGHT}, {"type": "rasterize"}, ] } if not is_training: config['threads'] = 1 return picpac.ImageStream(config)
def create_picpac_stream (db_path, is_training): assert os.path.exists(db_path) augments = [] if is_training: augments = [ {"type": "augment.flip", "horizontal": True, "vertical": True}, {"type": "augment.rotate", "min":-10, "max":10}, {"type": "augment.scale", "min":0.9, "max":1.1}, {"type": "augment.add", "range":20}, ] else: augments = [] config = {"db": db_path, "loop": is_training, "shuffle": is_training, "reshuffle": is_training, "annotate": True, "channels": FLAGS.channels, "stratify": is_training, "dtype": "float32", "batch": FLAGS.batch, "colorspace": COLORSPACE, "transforms": augments + [ {"type": "clip", "round": FLAGS.stride}, {"type": "rasterize"}, ] } if is_training and not FLAGS.mixin is None: print("mixin support is incomplete in new picpac.") # assert os.path.exists(FLAGS.mixin) # picpac_config['mixin'] = FLAGS.mixin # picpac_config['mixin_group_delta'] = 1 # pass return picpac.ImageStream(config)
def create_picpac_stream(db_path, is_training): assert os.path.exists(db_path) augments = [] shift = 0 if is_training: shift = FLAGS.shift if FLAGS.augments: with open(FLAGS.augments, 'r') as f: augments = json.loads(f.read()) print("Using augments:") print(json.dumps(augments)) else: augments = [ { "type": "augment.flip", "horizontal": True, "vertical": False }, ] config = { "db": db_path, "loop": is_training, "shuffle": is_training, "reshuffle": is_training, "annotate": False, "channels": FLAGS.channels, "stratify": is_training, "dtype": "float32", "batch": FLAGS.batch, "cache": FLAGS.cache, "transforms": augments + [ #{"type": "resize", "size": FLAGS.size}, { "type": "clip", "size": FLAGS.size, "shift": shift, "border_type": "replicate" }, ] } if is_training and not FLAGS.mixin is None: print("mixin support is incomplete in new picpac.") assert os.path.exists(FLAGS.mixin) config['mixin'] = FLAGS.mixin config['mixin_group_reset'] = 0 config['mixin_group_delta'] = 1 pass return picpac.ImageStream(config)
def main(_): assert FLAGS.out assert FLAGS.db and os.path.exists(FLAGS.db) picpac_config = dict( seed=2016, #loop=True, shuffle=True, reshuffle=True, #resize_width=256, #resize_height=256, round_div=FLAGS.stride, batch=1, split=1, split_fold=0, annotate='json', channels=FLAGS.channels, stratify=True, pert_color1=20, pert_angle=20, pert_min_scale=0.8, pert_max_scale=1.2, #pad=False, pert_hflip=True, pert_vflip=True, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) stream = picpac.ImageStream(FLAGS.db, perturb=False, loop=False, **picpac_config) gal = Gallery(FLAGS.out) cc = 0 with Model(FLAGS.model, name=FLAGS.name, prob=True) as model: for images, _, _ in stream: #images *= 600.0/1500 #images -= 800 #images *= 3000 /(2000-800) _, H, W, _ = images.shape if FLAGS.max_size: if max(H, W) > FLAGS.max_size: continue if FLAGS.patch: stch = Stitcher(images, FLAGS.patch) probs = stch.stitch(model.apply(stch.split())) else: probs = model.apply(images) cc += 1 save(gal.next(), images, probs) if FLAGS.max and cc >= FLAGS.max: break gal.flush() pass
def main(_): X = tf.placeholder(tf.float32, shape=(None, None, None, 3), name="images") is_training = tf.placeholder(tf.bool, name="is_training") anchor_th = tf.constant(FLAGS.anchor_th, tf.float32) nms_max = tf.constant(FLAGS.nms_max, tf.int32) nms_th = tf.constant(FLAGS.nms_th, tf.float32) model = Model(X, anchor_th, nms_max, nms_th, is_training, FLAGS.model, 'xxx') config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: model.loader(sess) if FLAGS.input: assert os.path.exists(FLAGS.input) image = cv2.imread(FLAGS.input, cv2.IMREAD_COLOR) batch = np.expand_dims(image, axis=0).astype(dtype=np.float32) boxes, probs = sess.run([model.boxes, model.probs], feed_dict={ X: batch, is_training: False }) save_prediction_image(FLAGS.input + '.prob.png', image, boxes, probs) if FLAGS.input_db: assert os.path.exists(FLAGS.input_db) import picpac from gallery import Gallery picpac_config = { "db": FLAGS.input_db, "loop": False, "shuffle": False, "reshuffle": False, "annotate": False, "channels": 3, "stratify": False, "dtype": "float32", "batch": 1, "transforms": [] } stream = picpac.ImageStream(picpac_config) gal = Gallery(FLAGS.input_db + '.out') C = 0 for _, images in stream: boxes, probs = sess.run([model.boxes, model.probs], feed_dict={ X: images, is_training: False }) save_prediction_image(gal.next(), images[0], boxes, probs) C += 1 if FLAGS.max and C >= FLAGS.max: break pass pass gal.flush() pass
def main (_): assert FLAGS.out assert FLAGS.db and os.path.exists(FLAGS.db) picpac_config = dict(seed=2016, #loop=True, shuffle=True, reshuffle=True, max_size = 400, #resize_width=256, #resize_height=256, round_div = FLAGS.stride, batch=1, split=1, split_fold=0, annotate='json', channels=FLAGS.channels, stratify=True, #pad=False, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) stream = picpac.ImageStream(FLAGS.db, perturb=False, loop=False, **picpac_config) gal = Gallery(FLAGS.out, score=True) cc = 0 with Model(FLAGS.model, prob=True) as model: for images, _, _ in stream: #images *= 600.0/1500 #images -= 800 #images *= 3000 /(2000-800) _, H, W, _ = images.shape if FLAGS.max_size: if max(H, W) > FLAGS.max_size: continue print(images.shape) # fcn-cls do not have patch # if FLAGS.patch: # stch = Stitcher(images, FLAGS.patch) # probs = stch.stitch(model.apply(stch.split())) # else: # probs = model.apply(images) probs, scores = model.apply(images) cc += 1 save(gal.next(score=scores[0]), images, probs) if FLAGS.max and cc >= FLAGS.max: break gal.flush(rank=True) pass
def __init__(self, path, **kwargs): super(ImageStream, self).__init__() kwargs['loop'] = False kwargs.setdefault('batch', 1) self.stream = picpac.ImageStream(path, **kwargs) self.batch_size = kwargs['batch'] self.data = None self.label = None self.pad = None self.peek = self.stream.next() self.provide_data = [('data', self.peek[0].shape)] self.provide_label = [('softmax_label', self.peek[1].shape)] pass
def create_picpac_stream(db_path, is_training): assert os.path.exists(db_path) augments = [] shift = 0 if is_training: shift = FLAGS.shift augments = [ { "type": "augment.flip", "horizontal": True, "vertical": False }, ] config = { "db": db_path, "loop": is_training, "shuffle": is_training, "reshuffle": is_training, "annotate": False, "channels": FLAGS.channels, "stratify": is_training, "dtype": "float32", "batch": FLAGS.batch, "transforms": augments + [ #{"type": "resize", "size": FLAGS.size}, { "type": "clip", "size": FLAGS.size, "shift": shift, "border_type": "replicate" }, ] } if is_training and not FLAGS.mixin is None: print("mixin support is incomplete in new picpac.") # assert os.path.exists(FLAGS.mixin) # picpac_config['mixin'] = FLAGS.mixin # picpac_config['mixin_group_delta'] = 1 # pass return picpac.ImageStream(config)
def main (_): assert FLAGS.db and os.path.exists(FLAGS.db) assert FLAGS.model and os.path.exists(FLAGS.model + '.meta') L = tf.placeholder(tf.float32, shape=(None, None, None, 1)) mg = meta_graph.read_meta_graph_file(FLAGS.model + '.meta') logits, = tf.import_graph_def(mg.graph_def, name='colorize', #input_map={'L:0':L}, input_map={'fifo_queue_Dequeue:0':L}, return_elements=['logits:0']) prob = tf.nn.softmax(logits) saver = tf.train.Saver(saver_def=mg.saver_def, name='colorize') picpac_config = dict(seed=2016, cache=False, max_size=200, min_size=192, crop_width=192, crop_height=192, shuffle=True, reshuffle=True, batch=1, round_div=FLAGS.stride, channels=3, stratify=False, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) stream = picpac.ImageStream(FLAGS.db, perturb=False, loop=False, **picpac_config) with tf.Session() as sess: tf.global_variables_initializer().run() saver.restore(sess, FLAGS.model) gallery = Gallery(FLAGS.output, cols=2, header=['groundtruth', 'prediction']) c = 0 for images, _, _ in stream: if FLAGS.max and (c >= FLAGS.max): break l, ab, w = _pic2pic.encode_lab(images.copy(), FLAGS.downsize) ab_p, = sess.run([prob], feed_dict={L: l}) y_p = decode_lab(l, ab_p, T=FLAGS.T) cv2.imwrite(gallery.next(), images[0]) cv2.imwrite(gallery.next(), y_p[0]) c += 1 print('%d/%d' % (c, FLAGS.max)) pass gallery.flush() pass pass
def main(_): X = tf.placeholder(tf.float32, shape=(None, None, None, 1), name="images") model = Model(X, FLAGS.model, 'xxx') config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) model.loader(sess) # 模型至此导入完毕 # 注意如果要读入图片预测,需要进行下述预处理 # 1. 转成灰度图, 比如用cv2.imread(..., cv2.IMREAD_GRAYSCALE) # 2. resize成固定大小,和train.sh中的参数匹配 # 3. 比如灰度图是image, 那么对应的batch = image[np.newaxis, :, :, np.newaxis], 就可以送入tf.session预测了 gal = Gallery('output', cols=2, ext='.jpg') CC = 0 stream = picpac.ImageStream({ 'db': FLAGS.db, 'loop': False, 'channels': 1, 'threads': 1, 'shuffle': False, 'transforms': [{ "type": "resize", "width": FLAGS.width, "height": FLAGS.height }] }) for meta, batch in stream: if CC > FLAGS.max: break print(meta.ids) image = batch[0] logits = sess.run(model.logits, feed_dict={X: batch}) # 把logits转换成字符串 label = logits2text(logits) '''END INFERENCE''' save_prediction_image(gal, image, label) CC += 1 gal.flush() pass
def create_picpac_stream (db_path, is_training): assert os.path.exists(db_path) augments = [] if is_training: if FLAGS.augments: with open(FLAGS.augments, 'r') as f: augments = json.loads(f.read()) print("Using augments:") print(json.dumps(augments)) else: augments = [ #{"type": "augment.flip", "horizontal": True, "vertical": False}, {"type": "augment.rotate", "min":-10, "max":10}, {"type": "augment.scale", "min":0.9, "max":1.1}, {"type": "augment.add", "range":20}, ] else: augments = [] picpac_config = {"db": db_path, "loop": is_training, "shuffle": is_training, "reshuffle": is_training, "annotate": True, "channels": FLAGS.channels, "stratify": is_training, "dtype": "float32", "batch": FLAGS.batch, "colorspace": COLORSPACE, "cache": FLAGS.cache, "transforms": augments + [ {"type": "clip", "round": FLAGS.backbone_stride}, {"type": "anchors.dense.box", 'downsize': FLAGS.anchor_stride}, {"type": "box_feature"}, {"type": "rasterize", "use_tag": True, "dtype": "float32"} ] } if is_training and not FLAGS.mixin is None: #print("mixin support is incomplete in new picpac.") assert os.path.exists(FLAGS.mixin) picpac_config['mixin'] = FLAGS.mixin picpac_config['mixin_group_reset'] = 0 picpac_config['mixin_group_delta'] = 1 return picpac.ImageStream(picpac_config)
def __init__(self, path, **kwargs): super(ImageStream, self).__init__() kwargs.setdefault('batch', 1) kwargs.setdefault('loop', True) self.stream = picpac.ImageStream(path, **kwargs) self.batch_size = kwargs['batch'] self.nclass = kwargs['onehot'] self.loop = kwargs['loop'] self.ndata = self.stream.size() self.peek = self.stream.next() self.shape = tuple(self.peek[0].shape[1:]) self.xdim = reduce(mul, self.shape) yshape = tuple(self.peek[1].shape[1:]) if self.nclass: assert len(yshape) == 1 self.ydim = reduce(mul, yshape) self.Xbuf = self.be.iobuf(self.xdim) self.Ybuf = self.be.iobuf(self.ydim) pass
def create_picpac_stream(path, is_training, extra_config): assert os.path.exists(path) print("CACHE:", FLAGS.cache) # check db size, warn not to cache if file is big statinfo = os.stat(path) if statinfo.st_size > 0x40000000 and FLAGS.cache: print_red( "DB is probably too big too be cached, consider adding --cache 0") config = { "db": path, "loop": is_training, "shuffle": is_training, "reshuffle": is_training, "annotate": [], "channels": FLAGS.channels, "stratify": is_training, "dtype": "float32", "batch": FLAGS.batch, "colorspace": FLAGS.colorspace, "cache": FLAGS.cache, "transforms": [] } if is_training: config[ "dump"] = FLAGS.picpac_dump # dump 20 training samples for debugging and see else: config['threads'] = 1 if is_training and not FLAGS.mixin is None: print("mixin support is incomplete in new picpac.") assert os.path.exists(FLAGS.mixin) config['mixin'] = FLAGS.mixin config['mixin_group_reset'] = 0 config['mixin_group_delta'] = 1 pass config.update(extra_config) return picpac.ImageStream(config)
def main(_): picpac_config = dict( seed=2016, shuffle=True, batch=1, annotate='json', channels=1, perturb=False, loop=False, stratify=True, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) tr_stream = picpac.ImageStream(FLAGS.db, **picpac_config) gal = Gallery('sample', ext='.gif') cc = 0 for image, label, _ in tr_stream: cc += 1 visualize(gal.next(), image[0], label[0, :, :, 0]) if cc >= FLAGS.classes - 1: break pass gal.flush()
def main(_): logging.basicConfig(level=FLAGS.verbose) try: os.makedirs(FLAGS.model) except: pass assert FLAGS.pos and os.path.exists(FLAGS.pos) assert FLAGS.neg and os.path.exists(FLAGS.neg) X = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels), name="images") Y_fcn = tf.placeholder(tf.float32, shape=(None, None, None, 1)) Y_cls = tf.placeholder(tf.float32, shape=(None, )) with slim.arg_scope([slim.conv2d, slim.conv2d_transpose, slim.max_pool2d], padding=FLAGS.padding): logits_fcn, logits_cls, stride = getattr(fcn_cls_nets, FLAGS.net)(X) loss_fcn = fcn_loss(logits_fcn, Y_fcn) loss_cls, accuracy_cls = cls_loss(logits_cls, Y_cls) #tf.summary.scalar("loss", loss) loss_total = tf.identity(loss_fcn + FLAGS.W * loss_cls, name='loss') metrics = [loss_total, loss_fcn, loss_cls, accuracy_cls] metric_names = [x.name[:-2] for x in metrics] rate = FLAGS.learning_rate if FLAGS.opt == 'adam': rate /= 100 global_step = tf.Variable(0, name='global_step', trainable=False) if FLAGS.decay: rate = tf.train.exponential_decay(rate, global_step, FLAGS.decay_steps, FLAGS.decay_rate, staircase=True) if FLAGS.opt == 'adam': optimizer = tf.train.AdamOptimizer(rate) elif FLAGS.opt == 'mom': optimizer = tf.train.MomentumOptimizer(rate, FLAGS.momentum) else: optimizer = tf.train.GradientDescentOptimizer(rate) pass train_op = optimizer.minimize(loss_total, global_step=global_step) picpac_config_cls = dict( seed=2016, shuffle=True, reshuffle=True, #max_size = 300, #resize_width=256, #resize_height=256, split=FLAGS.split, split_fold=FLAGS.split_fold, batch=1, pert_angle=15, pert_hflip=True, pert_vflip=False, pert_color1=20, pert_color2=20, pert_color3=20, pert_min_scale=0.8, pert_max_scale=1.2, channels=FLAGS.channels, #mixin = FLAGS.mixin, stratify=True, #pad=False, channel_first=False, # this is tensorflow specific ) picpac_config_fcn = dict( annotate='json', round_div=stride, ) picpac_config_fcn.update(picpac_config_cls) tr_stream0 = picpac.ImageStream(FLAGS.neg, split_negate=False, perturb=True, loop=True, **picpac_config_fcn) tr_stream1 = picpac.ImageStream(FLAGS.pos, split_negate=False, perturb=True, loop=True, **picpac_config_fcn) te_streams = [] if FLAGS.test_epochs > 0: # testing stream, "negate" inverts the image selection specified by split & split_fold # so different images are used for training and testing if FLAGS.test_db: if FLAGS.split > 1: print( "Cannot use cross-validation & evaluation db at the same time" ) print("If --test-db is specified, do not set --split") raise Exception("bad parameters") te_streams.append((0, picpac.ImageStream(FLAGS.test_db, perturb=False, loop=False, **picpac_config_cls))) elif FLAGS.split > 1: te_streams.append((0, picpac.ImageStream(FLAGS.neg, split_negate=True, perturb=False, loop=False, **picpac_config_cls))) te_streams.append((1, picpac.ImageStream(FLAGS.pos, split_negate=True, perturb=False, loop=False, **picpac_config_cls))) pass pass init = tf.global_variables_initializer() saver = tf.train.Saver(max_to_keep=FLAGS.max_to_keep) config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.3 with tf.Session(config=config) as sess: sess.run(init) if FLAGS.resume: saver.restore(sess, FLAGS.resume) step = 0 epoch = 0 global_start_time = time.time() while step < FLAGS.max_steps: start_time = time.time() avg = np.array([0] * len(metrics), dtype=np.float32) for _ in tqdm(range(FLAGS.epoch_steps), leave=False): # train FCN images, labels, _ = tr_stream0.next() #print('xxx', images.shape) feed_dict = {X: images, Y_fcn: labels, Y_cls: [0]} mm, _ = sess.run([metrics, train_op], feed_dict=feed_dict) avg += np.array(mm) images, labels, _ = tr_stream1.next() #print('xxx', images.shape) feed_dict = {X: images, Y_fcn: labels, Y_cls: [1]} mm, _ = sess.run([metrics, train_op], feed_dict=feed_dict) avg += np.array(mm) step += 1 pass avg /= FLAGS.epoch_steps * 2 stop_time = time.time() txt = ', '.join( ['%s=%.4f' % (a, b) for a, b in zip(metric_names, list(avg))]) print('step %d: elapsed=%.4f time=%.4f, %s' % (step, (stop_time - global_start_time), (stop_time - start_time), txt)) epoch += 1 if epoch and (epoch % FLAGS.ckpt_epochs == 0): ckpt_path = '%s/%d' % (FLAGS.model, step) start_time = time.time() saver.save(sess, ckpt_path) stop_time = time.time() print('epoch %d step %d, saving to %s in %.4fs.' % (epoch, step, ckpt_path, stop_time - start_time)) if epoch and (epoch % FLAGS.test_epochs == 0) and len(te_streams) > 0: cmatrix = np.zeros((2, 2)) total = 0 acc_sum = 0 for delta, te_stream in te_streams: te_stream.reset() for images, labels, _ in te_stream: total += 1 labels += delta feed_dict = {X: images, Y_cls: labels} acc, ll = sess.run([accuracy_cls, logits_cls], feed_dict=feed_dict) acc_sum += acc cmatrix[int(labels), np.where(ll == np.max(ll))[1]] += np.divide( float(1), np.size(np.where(ll == np.max(ll))[1])) pass rowsum = np.sum(cmatrix, axis=1) colsum = np.sum(cmatrix, axis=0) #print(total) #print(np.tile(rowsum,(FLAGS.classes,1)).transpose()) #print(np.tile(colsum,(FLAGS.classes,1))) print('row---label; colum ---predict') print(total) print('evaluation: accuracy = %.4f' % (acc_sum / total)) print('accuracy from confusion matrix = %.4f' % np.divide(np.trace(cmatrix), float(total))) print('absolute confusion matrix:') print(cmatrix) #print('confusion matrix divided by total:') #print(np.divide(cmatrix,float(total))) print('confusion matrix divided by col sum:') print(np.divide(cmatrix, np.tile(colsum, (2, 1)))) print('confusion matrix divided by row sum:') print(np.divide(cmatrix, np.tile(rowsum, (2, 1)).transpose())) pass pass pass
def main(_): logging.basicConfig(level=FLAGS.verbose) try: os.makedirs(FLAGS.model) except: pass assert FLAGS.db and os.path.exists(FLAGS.db) rate = tf.constant(FLAGS.learning_rate) global_step = tf.Variable(0, name='global_step', trainable=False) if FLAGS.decay: rate = tf.train.exponential_decay(rate, global_step, FLAGS.decay_steps, FLAGS.decay_rate, staircase=True) tf.summary.scalar('learning_rate', rate) optimizer = tf.train.AdamOptimizer(rate) enc_GRAY = tf.placeholder(tf.float32, shape=(None, None, None, 1)) # inference input enc_GTCOLOR = tf.placeholder(tf.float32, shape=(None, None, None, 3)) # inference output enc_W = tf.placeholder(tf.float32, shape=(None, None, None, 1)) # inference per-pixel weight queue = tf.FIFOQueue(128, (tf.float32, tf.float32, tf.float32)) enc = queue.enqueue((enc_GRAY, enc_GTCOLOR, enc_W)) GRAY, GTCOLOR, W = queue.dequeue() GRAY.set_shape(enc_GRAY.get_shape()) # we need to name this so as to map the variable for prediction GRAY = tf.identity(GRAY, name='gray') GTCOLOR.set_shape(enc_GTCOLOR.get_shape()) W.set_shape(enc_W.get_shape()) COLOR, stride, downsize = generator(GRAY) COLOR = tf.identity(COLOR, name='color') #tf.summary.image('low_res', BGR2RGB(X), max_outputs=3) #tf.summary.image('hi_res', BGR2RGB(Y), max_outputs=3) #tf.summary.image('predict', BGR2RGB(G), max_outputs=3) tf.summary.image('input', BGR2RGB(GTCOLOR), max_outputs=5) tf.summary.image('output', BGR2RGB(COLOR), max_outputs=5) # X and G need to go through the same feature extracting network # so we concatenate them first and then split the results L, H = feature_extractor(tf.concat([GTCOLOR, COLOR], axis=0)) L1, L2 = tf.split(L, 2) # low-level feature, 2 is prediction H1, H2 = tf.split(H, 2) # high-level feature, 2 is prediction loss_perceptual = tf.reduce_mean(tf.square(L1 - L2), name='pe') sub = tf.shape(COLOR) - tf.constant([0, 1, 1, 0], dtype=tf.int32) G0 = tf.slice(COLOR, [0, 0, 0, 0], sub) Gx = tf.slice(COLOR, [0, 0, 1, 0], sub) Gy = tf.slice(COLOR, [0, 1, 0, 0], sub) loss_smooth = tf.identity( tf.reduce_mean(tf.pow(tf.square(G0 - Gx) + tf.square(G0 - Gy), 1.25)) / 529357.9139706489, name='sm') D_real = tf.reduce_mean(tf.nn.softplus(H1), name='dr') D_fake = tf.reduce_mean(softminus(H2), name='df') loss_adversary = tf.identity(1.0 - D_fake, name='ad') loss_G = tf.identity(loss_perceptual * FLAGS.perceptual_weight + loss_smooth * FLAGS.smoothness_weight + loss_adversary * FLAGS.adversary_weight, name='lg') loss_D = tf.identity(D_fake - D_real, name='ld') phases = [] var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "G") phases.append(('generate', optimizer.minimize(loss_G, global_step=global_step, var_list=var_list), [loss_G, loss_perceptual, loss_smooth, loss_adversary], [GTCOLOR, COLOR])) var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "D") phases.append( ('discriminate', optimizer.minimize(loss_D, global_step=global_step, var_list=var_list), [loss_D, D_real, D_fake], [])) metric_names = [] for _, _, metrics, _ in phases: metric_names.extend([x.name[:-2] for x in metrics]) init = tf.global_variables_initializer() saver = tf.train.Saver(max_to_keep=FLAGS.max_to_keep) summaries = tf.summary.merge_all() if FLAGS.resume is None: if FLAGS.log[0] != '/': subprocess.check_call("rm -rf %s" % FLAGS.log, shell=True) log = tf.summary.FileWriter(FLAGS.log, tf.get_default_graph(), flush_secs=20) tf.get_default_graph().finalize() picpac_config = dict( seed=2016, cache=False, max_size=200, min_size=192, crop_width=176, crop_height=176, shuffle=True, reshuffle=True, batch=FLAGS.batch, round_div=stride, channels=3, stratify=False, pert_min_scale=0.97, #0.92, pert_max_scale=1.5, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) stream = picpac.ImageStream(FLAGS.db, perturb=True, loop=True, **picpac_config) sess_config = tf.ConfigProto() with tf.Session(config=sess_config) as sess: coord = tf.train.Coordinator() sess.run(init) if FLAGS.resume: saver.restore(sess, FLAGS.resume) def encode_sample(): while not coord.should_stop(): images, _, _ = stream.next() gray, color, w = _pic2pic.encode_bgr(images, downsize) #print("GRAY:", gray.shape) #print("COLOR:", color.shape) sess.run([enc], feed_dict={ enc_GRAY: gray, enc_GTCOLOR: color, enc_W: w }) pass # create encoding threads threads = [ threading.Thread(target=encode_sample, args=()) for _ in range(FLAGS.encoding_threads) ] for t in threads: t.start() step = 0 epoch = 0 global_start_time = time.time() while step < FLAGS.max_steps: start_time = time.time() avg = np.array([0] * len(metric_names), dtype=np.float32) for _ in tqdm(range(FLAGS.epoch_steps), leave=False): forward_dict = {} m_off = 0 for _, train, metrics, forward in phases: feed_dict = {} feed_dict.update(forward_dict) _, m, f = sess.run([train, metrics, forward], feed_dict=feed_dict) forward_dict = dict(zip(forward, f)) m_off_n = m_off + len(metrics) avg[m_off:m_off_n] += m m_off = m_off_n pass step += 1 pass images, _, _ = stream.next() images = images[:5, :, :, :] gray, gtcolor, _ = _pic2pic.encode_bgr(images, downsize) color = sess.run(COLOR, feed_dict={GRAY: gray}) full = np.zeros(images.shape, dtype=np.float32) _, H, W, _ = images.shape color /= 255.0 gray /= 255.0 for i in range(images.shape[0]): lab = cv2.cvtColor(cv2.cvtColor(gray[i], cv2.COLOR_GRAY2BGR), cv2.COLOR_BGR2LAB) full[i, :, :, :1] = lab[:, :, :1] one = cv2.resize(color[i], (W, H)) lab = cv2.cvtColor(one, cv2.COLOR_BGR2LAB) full[i, :, :, 1:] = lab[:, :, 1:] cv2.cvtColor(full[i], cv2.COLOR_LAB2BGR, full[i]) pass s, = sess.run([summaries], feed_dict={ COLOR: full, GTCOLOR: images }) log.add_summary(s, step) avg /= FLAGS.epoch_steps stop_time = time.time() epoch += 1 saved = '' if epoch % FLAGS.ckpt_epochs == 0: saver.save(sess, '%s/%d' % (FLAGS.model, step)) saved = ' saved' txt = ', '.join( ['%s=%.4f' % (a, b) for a, b in zip(metric_names, list(avg))]) print('step=%d elapsed=%.4f/%.4f %s%s' % (step, (stop_time - start_time), (stop_time - global_start_time), txt, saved)) pass pass coord.request_stop() coord.join(threads) log.close() pass
def main(_): logging.basicConfig(level=FLAGS.verbose) try: os.makedirs(FLAGS.model) except: pass assert FLAGS.A and os.path.exists(FLAGS.A) assert FLAGS.B and os.path.exists(FLAGS.B) A = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels), name="A") B = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels), name="B") rate = tf.constant(FLAGS.learning_rate) if FLAGS.opt == 'adam': rate /= 100 global_step = tf.Variable(0, name='global_step', trainable=False) if FLAGS.decay: rate = tf.train.exponential_decay(rate, global_step, FLAGS.decay_steps, FLAGS.decay_rate, staircase=True) tf.summary.scalar('learning_rate', rate) if FLAGS.opt == 'adam': optimizer = tf.train.AdamOptimizer(rate) elif FLAGS.opt == 'mom': optimizer = tf.train.MomentumOptimizer(rate, FLAGS.momentum) else: optimizer = tf.train.GradientDescentOptimizer(rate) pass phases = build_graph(A, B, optimizer, optimizer, global_step) metric_names = [] for _, _, metrics, _, _ in phases: metric_names.extend([x.name[:-2] for x in metrics]) summaries = tf.constant(1) summary_writer = None if FLAGS.log: summaries = tf.summary.merge_all() assert not summaries is None summary_writer = tf.summary.FileWriter(FLAGS.log, tf.get_default_graph(), flush_secs=20) saver = tf.train.Saver(max_to_keep=FLAGS.max_to_keep) init = tf.global_variables_initializer() tf.get_default_graph().finalize() graph = tf.get_default_graph() graph.finalize() graph_def = graph.as_graph_def() for node in graph_def.node: if node.name[0] == 'D': #print(node.name) pass picpac_config = dict( seed=2016, shuffle=True, reshuffle=True, batch=1, split=1, split_fold=0, round_div=FLAGS.stride, channels=FLAGS.channels, stratify=True, pert_color1=20, pert_angle=20, pert_min_scale=0.9, pert_max_scale=1.5, pert_hflip=True, pert_vflip=True, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) streamA = picpac.ImageStream(FLAGS.A, perturb=True, loop=True, **picpac_config) streamB = picpac.ImageStream(FLAGS.B, perturb=True, loop=True, **picpac_config) sess_config = tf.ConfigProto() sess_config.gpu_options.allow_growth = True with tf.Session(config=sess_config) as sess: sess.run(init) if FLAGS.resume: saver.restore(sess, FLAGS.resume) step = 0 epoch = 0 global_start_time = time.time() while step < FLAGS.max_steps: start_time = time.time() avg = np.array([0] * len(metric_names), dtype=np.float32) for _ in tqdm(range(FLAGS.epoch_steps), leave=False): a, _, _ = streamA.next() b, _, _ = streamB.next() forward_dict = {} m_off = 0 for _, train, metrics, forward, show in phases: feed_dict = {A: a, B: b} feed_dict.update(forward_dict) _, m, f, ss = sess.run([train, metrics, forward, show], feed_dict=feed_dict) forward_dict = dict(zip(forward, f)) m_off_n = m_off + len(metrics) avg[m_off:m_off_n] += m m_off = m_off_n pass step += 1 pass avg /= FLAGS.epoch_steps stop_time = time.time() txt = ', '.join( ['%s=%.4f' % (a, b) for a, b in zip(metric_names, list(avg))]) print('step %d: elapsed=%.4f time=%.4f %s' % (step, (stop_time - global_start_time), (stop_time - start_time), txt)) if summary_writer: s, = sess.run([summaries], feed_dict=feed_dict) summary_writer.add_summary(s, step) epoch += 1 if epoch and (epoch % FLAGS.ckpt_epochs == 0): ckpt_path = '%s/%d' % (FLAGS.model, step) start_time = time.time() saver.save(sess, ckpt_path) stop_time = time.time() print('epoch %d step %d, saving to %s in %.4fs.' % (epoch, step, ckpt_path, stop_time - start_time)) pass pass if summary_writer: summary_writer.close() pass
def run_training(start_time): picpac_config = dict( seed=2016, #loop=True, shuffle=True, reshuffle=True, # max_size = 400, resize_width=FLAGS.img_size, resize_height=FLAGS.img_size, batch=FLAGS.batch_size, pert_angle=5, pert_hflip=True, pert_vflip=False, channels=3, # mixin = None # mixin_group_delta=1, # stratify=True, #pad=False, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) if FLAGS.train_db: tr_stream = picpac.ImageStream(FLAGS.train_db, perturb=False, loop=True, **picpac_config) tr_val_stream = picpac.ImageStream(FLAGS.train_db, perturb=False, loop=False, **picpac_config) else: return if FLAGS.test_db: te_stream = picpac.ImageStream(FLAGS.test_db, perturb=False, loop=False, **picpac_config) # use VGG model slim = tf.contrib.slim # Tell TensorFlow that the model will be built into the default Graph. with tf.Graph().as_default(): # Input images and labels. images_placeholder = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, FLAGS.img_size, FLAGS.img_size, 3), name='images') labels_placeholder = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, ), name='labels') # model inference, loss and accuracy predictions, _ = inference(images_placeholder, FLAGS.num_classes, FLAGS.net) loss, accuracy = fcn_loss(predictions, labels_placeholder) # Prediction results in a batch. top_k_op = tf.nn.in_top_k(predictions, tf.to_int32(labels_placeholder), 1, name="accuracy") tf.summary.scalar('xentropy_mean', loss) tf.summary.scalar('accuracy_mean', accuracy) # Specify the optimization scheme: optimizer = tf.train.GradientDescentOptimizer( learning_rate=FLAGS.learning_rate) # create_train_op that ensures that when we evaluate it to get the loss, # the update_ops are done and the gradient updates are computed. train_op = optimizer.minimize(loss) tf.summary.scalar('learning_rate', FLAGS.learning_rate) tf.summary.scalar('batch_size', FLAGS.batch_size) # tf.summary only track the changes of a tensor. # Our accuracy is given by a scaler number. # So create two tf.Variable objects to summarize evaluation results # initial evaluation accuracy to 0.5 eval_tr = tf.Variable(0.5, name="eval_validationdata") eval_te = tf.Variable(0.5, name="eval_trainingdata") eval_s1 = tf.summary.scalar('eval_validationdata', eval_te) eval_s2 = tf.summary.scalar('eval_trainingdata', eval_tr) # The op for initializing the variables. init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # Create a saver for writing training checkpoints. saver = tf.train.Saver(max_to_keep=1000) # Build the summary Tensor based on the TF collection of Summaries summary_op = tf.summary.merge_all() # Instantiate a SummaryWriter to output summaries and the Graph summary_writer = tf.summary.FileWriter(FLAGS.log_dir, tf.get_default_graph()) config = tf.ConfigProto() # config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.3 loss_sum = 0 acc_sum = 0 batch_sum = 0 with tf.Session(config=config) as sess: sess.run(init_op) # Start input enqueue threads. # coord = tf.train.Coordinator() # threads = tf.train.start_queue_runners(coord=coord) for step in xrange(FLAGS.max_steps + 1): # Get value of a batch of training images and labels images, labels, pad = tr_stream.next() feed_dict = { images_placeholder: images, labels_placeholder: labels } # start training _, loss_value, acc_value = sess.run([train_op, loss, accuracy], feed_dict=feed_dict) loss_sum += loss_value * FLAGS.batch_size acc_sum += acc_value * FLAGS.batch_size batch_sum += FLAGS.batch_size # Print an overview fairly often. if step % 100 == 0: duration = time.time() - start_time print('Step %d: loss = %.4f, accuracy = %.4f (%.3f sec)' % (step, loss_sum / batch_sum, acc_sum / batch_sum, duration)) summary_str = sess.run(summary_op, feed_dict=feed_dict) summary_writer.add_summary(summary_str, step) summary_writer.flush() loss_sum = 0 acc_sum = 0 batch_sum = 0 # Save a checkpoint and evaluate the model periodically if step != 0: if step % FLAGS.save_steps == 0 or step == FLAGS.max_steps: checkpoint_file = os.path.join(FLAGS.log_dir, 'model.ckpt') saver.save(sess, checkpoint_file, global_step=step) # Evaluate against the training set tr_val_stream.reset() batch_sum2 = 0 acc_sum2 = 0 loss_sum2 = 0 for images, labels, pad in tr_val_stream: bs = FLAGS.batch_size - pad if pad > 0: np.resize(images, (bs, ) + images.shape[1:]) np.resize(labels, (bs, )) feed_dict = { images_placeholder: images, labels_placeholder: labels } loss_value, acc_value = sess.run( [loss, accuracy], feed_dict=feed_dict) batch_sum2 += bs loss_sum2 += loss_value * bs acc_sum2 += acc_value * bs pass print( 'Step %d: training loss = %.4f, accuracy = %.4f (%.f sec)' % (step, loss_sum2 / batch_sum2, acc_sum2 / batch_sum2, time.time() - start_time)) # Evaluate against the validation set te_stream.reset() batch_sum3 = 0 acc_sum3 = 0 loss_sum3 = 0 for images, labels, pad in te_stream: bs = FLAGS.batch_size - pad if pad > 0: np.resize(images, (bs, ) + images.shape[1:]) np.resize(labels, (bs, )) feed_dict = { images_placeholder: images, labels_placeholder: labels } loss_value, acc_value = sess.run( [loss, accuracy], feed_dict=feed_dict) batch_sum3 += bs loss_sum3 += loss_value * bs acc_sum3 += acc_value * bs pass print( 'Step %d: validation loss = %.4f, accuracy = %.4f (%.f sec)' % (step, loss_sum3 / batch_sum3, acc_sum3 / batch_sum3, time.time() - start_time)) # update validation results sess.run(eval_tr.assign(acc_sum2 / batch_sum2)) sess.run(eval_te.assign(acc_sum3 / batch_sum3)) # print(eval_tr.eval()) # print(eval_te.eval()) merged_str = sess.run(tf.summary.merge( [eval_s1, eval_s2]), feed_dict=feed_dict) summary_writer.add_summary(merged_str, step) summary_writer.flush() print('Done training for %d steps.' % (FLAGS.max_steps))
def main(_): X = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels), name="images") model = Model(X, FLAGS.model, 'xxx') config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) model.loader(sess) gal = Gallery('output', cols=2, ext='.jpg') CC = 0 if FLAGS.list: with open(FLAGS.list, 'r') as f: for path in f: if CC > FLAGS.max: break path = path.strip() print(path) if FLAGS.channels == 3: image = cv2.imread(path, cv2.IMREAD_COLOR) elif FLAGS.channels == 1: image = cv2.imread(path, cv2.IMREAD_GRAYSCALE) image = np.expand_dims(image, axis=3) else: assert False H, W = image.shape[:2] if max(H, W) > FLAGS.max_size: f = FLAGS.max_size / max(H, W) image = cv2.resize(image, None, fx=f, fy=f) H, W = image.shape[:2] '''BEGIN INFERENCE''' # clip edge H = H // FLAGS.clip_stride * FLAGS.clip_stride W = W // FLAGS.clip_stride * FLAGS.clip_stride image = image[:H, :W].astype(np.float32) # change from BGR to RGB if FLAGS.channels == 3 and FLAGS.colorspace == 'RGB': image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) batch = np.expand_dims(image_rgb, axis=0) else: batch = np.expand_dims(image, axis=0) prob = sess.run(model.prob, feed_dict={X: batch}) '''END INFERENCE''' save_prediction_image(gal, image, prob[0]) CC += 1 if FLAGS.db: stream = picpac.ImageStream({ 'db': FLAGS.db, 'loop': False, 'channels': FLAGS.channels, 'colorspace': FLAGS.colorspace, 'threads': 1, 'shuffle': False, 'transforms': [{ "type": "resize", "max_size": FLAGS.max_size }, { "type": "clip", "round": FLAGS.clip_stride }] }) for meta, batch in stream: if CC > FLAGS.max: break print(meta.ids) image = batch[0] if FLAGS.channels == 3 and FLAGS.colorspace == 'RGB': image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) prob = sess.run(model.prob, feed_dict={X: batch}) '''END INFERENCE''' save_prediction_image(gal, image, prob[0]) CC += 1 gal.flush() pass
def eval(): picpac_config = dict( seed=2016, shuffle=False, reshuffle=False, # max_size = 400, # resize_width=FLAGS.img_size, # resize_height=FLAGS.img_size, batch=1, pert_angle=5, pert_hflip=True, pert_vflip=False, channels=3, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) te_stream = picpac.ImageStream(FLAGS.test_db, perturb=False, loop=False, **picpac_config) with tf.Graph().as_default(): images_placeholder = tf.placeholder(tf.float32, shape=(1, None, None, 3), name='images') labels_placeholder = tf.placeholder(tf.int32, shape=(1, ), name='labels') resized_images = tf.image.resize_images(images_placeholder, size=(FLAGS.img_size, FLAGS.img_size)) # inference model logits, _ = inference(resized_images, FLAGS.num_classes, FLAGS.net, is_training=False) scores = tf.nn.softmax(logits, dim=-1, name=None) # Calculate predictions top_k_op = tf.nn.in_top_k(predictions=logits, targets=labels_placeholder, k=FLAGS.num_classes - 1, name="accuracy") checkpoint_file = os.path.join(FLAGS.checkpoint_dir, 'model.ckpt-' + FLAGS.ckpt_step) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, checkpoint_file) htmlf = open(os.path.join(FLAGS.outdir, FLAGS.html), 'w') num = 0 true_count = 0 for images, labels, pad in te_stream: predictions, correct = sess.run([scores, top_k_op], feed_dict={ images_placeholder: images, labels_placeholder: labels }) print('image %s score: %s, correct: %s' % (num, predictions, np.sum(correct))) # save images path = os.path.join(FLAGS.outdir, FLAGS.imagedir, str(num) + '.jpg') cv2.imwrite(path, images[0]) # wirte to html htmlf.write( "<div style='float:left; width:400;height:350;'><img src={} width='300'><br>score={}<br>groundtruth={}<br>prediction={}</div>\n" .format(os.path.join(FLAGS.imagedir, str(num) + '.jpg'), predictions[0][1], labels[0], correct[0])) num += 1 true_count += np.sum(correct) # Compute precisions precision = true_count / num print('precision = {}'.format(precision)) htmlf.write('<br>total accuracy = {0:.5}\n'.format(precision)) htmlf.close()
def main(_): X = tf.placeholder(tf.float32, shape=(None, None, None, 3), name="images") anchor_th = tf.constant(FLAGS.anchor_th, tf.float32) nms_max = tf.constant(FLAGS.nms_max, tf.int32) nms_th = tf.constant(FLAGS.nms_th, tf.float32) model = Model(X, anchor_th, nms_max, nms_th, FLAGS.model, 'xxx') config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: model.loader(sess) if FLAGS.input: assert os.path.exists(FLAGS.input) image = cv2.imread(FLAGS.input, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) batch = np.expand_dims(image, axis=0).astype(dtype=np.float32) preds = sess.run(model.predictions, feed_dict={X: batch}) save_prediction_image(FLAGS.input + '.prob.png', image, preds) if FLAGS.input_db: assert os.path.exists(FLAGS.input_db) import picpac from gallery import Gallery picpac_config = { "db": FLAGS.input_db, "loop": False, "shuffle": False, "reshuffle": False, "annotate": False, "channels": 3, "stratify": False, "dtype": "float32", "colorspace": "RGB", "batch": 1, "transforms": [ { "type": "resize", "max_size": FLAGS.max_size, "min_size": FLAGS.min_size }, { "type": "clip", "round": FLAGS.stride }, ] } stream = picpac.ImageStream(picpac_config) gal = Gallery('output') C = 0 for _, images in stream: preds = sess.run(model.predictions, feed_dict={X: images}) save_prediction_image(gal.next(), images[0], preds) C += 1 if FLAGS.max and C >= FLAGS.max: break pass pass gal.flush() pass
def main(_): logging.basicConfig(level=FLAGS.verbose) try: os.makedirs(FLAGS.model) except: pass assert FLAGS.db and os.path.exists(FLAGS.db) X = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels), name="images") Y = tf.placeholder(tf.float32, shape=(None, None, None, 1), name="labels") with slim.arg_scope([slim.conv2d, slim.conv2d_transpose, slim.max_pool2d], padding='SAME'): logits, stride = getattr(nets, FLAGS.net)(X, num_classes=FLAGS.classes) loss, metrics = fcn_loss(logits, Y) #tf.summary.scalar("loss", loss) metric_names = [x.name[:-2] for x in metrics] for x in metrics: tf.summary.scalar(x.name.replace(':', '_'), x) rate = FLAGS.learning_rate if FLAGS.opt == 'adam': rate /= 100 global_step = tf.Variable(0, name='global_step', trainable=False) if FLAGS.decay: rate = tf.train.exponential_decay(rate, global_step, FLAGS.decay_steps, FLAGS.decay_rate, staircase=True) tf.summary.scalar('learning_rate', rate) if FLAGS.opt == 'adam': optimizer = tf.train.AdamOptimizer(rate) elif FLAGS.opt == 'mom': optimizer = tf.train.MomentumOptimizer(rate, FLAGS.momentum) else: optimizer = tf.train.GradientDescentOptimizer(rate) pass train_op = optimizer.minimize(loss, global_step=global_step) summary_writer = None train_summaries = tf.constant(1) #val_summaries = tf.constant(1) if FLAGS.log: train_summaries = tf.summary.merge_all() assert not train_summaries is None if not train_summaries is None: summary_writer = tf.summary.FileWriter(FLAGS.log, tf.get_default_graph(), flush_secs=20) #assert train_summaries #val_summaries = tf.summary.merge_all(key='val_summaries') picpac_config = dict( seed=2016, shuffle=True, reshuffle=True, batch=1, split=1, split_fold=0, round_div=stride, annotate='json', channels=FLAGS.channels, stratify=True, pert_color1=20, pert_angle=180, pert_min_scale=0.8, pert_max_scale=1.2, pert_hflip=True, pert_vflip=True, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) if not FLAGS.mixin is None: picpac_config['mixin'] = FLAGS.mixin picpac_config['mixin_group_delta'] = 1 tr_stream = picpac.ImageStream(FLAGS.db, perturb=True, loop=True, **picpac_config) val_stream = None if FLAGS.val: assert os.path.exists(FLAGS.val) val_stream = picpac.ImageStream(FLAGS.val, perturb=False, loop=False, **picpac_config) init = tf.global_variables_initializer() saver = tf.train.Saver(max_to_keep=FLAGS.max_to_keep) config = tf.ConfigProto() config.gpu_options.allow_growth = True # check if padding have been properly setup # this should be ensured if all layers are added via slim graph = tf.get_default_graph() graph.finalize() graph_def = graph.as_graph_def() with tf.Session(config=config) as sess: sess.run(init) if FLAGS.resume: saver.restore(sess, FLAGS.resume) step = 0 epoch = 0 global_start_time = time.time() while step < FLAGS.max_steps: start_time = time.time() avg = np.array([0] * len(metrics), dtype=np.float32) for _ in tqdm(range(FLAGS.epoch_steps), leave=False): images, labels, _ = tr_stream.next() feed_dict = {X: images, Y: labels} mm, _, summaries = sess.run( [metrics, train_op, train_summaries], feed_dict=feed_dict) avg += np.array(mm) step += 1 pass avg /= FLAGS.epoch_steps stop_time = time.time() txt = ', '.join( ['%s=%.4f' % (a, b) for a, b in zip(metric_names, list(avg))]) print('step %d: elapsed=%.4f time=%.4f, %s' % (step, (stop_time - global_start_time), (stop_time - start_time), txt)) if summary_writer: summary_writer.add_summary(summaries, step) epoch += 1 if epoch and (epoch % FLAGS.ckpt_epochs == 0): ckpt_path = '%s/%d' % (FLAGS.model, step) start_time = time.time() saver.save(sess, ckpt_path) stop_time = time.time() print('epoch %d step %d, saving to %s in %.4fs.' % (epoch, step, ckpt_path, stop_time - start_time)) if epoch and (epoch % FLAGS.val_epochs == 0) and val_stream: val_stream.reset() avg = np.array([0] * len(metrics), dtype=np.float32) cc = 0 for images, labels, _ in val_stream: _, H, W, _ = images.shape if FLAGS.max_size: if max(H, W) > FLAGS.max_size: continue if FLAGS.padding == 'SAME' and FLAGS.clip: images = clip(images, stride) labels = clip(labels, stride) feed_dict = {X: images, Y: labels} #print("XXX", images.shape) mm, = sess.run([metrics], feed_dict=feed_dict) avg += np.array(mm) cc += 1 avg /= cc txt = ', '.join([ '%s=%.4f' % (a, b) for a, b in zip(metric_names, list(avg)) ]) print('epoch %d step %d, validation %s' % (epoch, step, txt)) pass pass if summary_writer: summary_writer.close() pass
def main (_): try: os.makedirs(FLAGS.model) except: pass X = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels), name="images") # ground truth labels Y = tf.placeholder(tf.float32, shape=(None, ), name="labels") is_training = tf.placeholder(tf.bool, name="is_training") # load network logits = getattr(nets, FLAGS.net)(X, is_training, FLAGS.classes) loss, metrics = fcn_loss(logits, Y) metric_names = [x.name[:-2] for x in metrics] global_step = tf.Variable(0, name='global_step', trainable=False) rate = FLAGS.learning_rate rate = tf.train.exponential_decay(rate, global_step, FLAGS.decay_steps, FLAGS.decay_rate, staircase=True) optimizer = tf.train.AdamOptimizer(rate) train_op = optimizer.minimize(loss, global_step=global_step) init = tf.global_variables_initializer() saver = tf.train.Saver(max_to_keep=FLAGS.max_to_keep) picpac_config = dict(seed=2017, cache=True, # set to False for large datasets shuffle=True, reshuffle=True, batch=FLAGS.batch, channels=FLAGS.channels, # 3 by default stratify=True, pert_colorspace='SAME', # do not change colorspace # which typically means BGR # or HSV or Lab pert_color1=20, pert_color2=20, pert_color3=20, pert_angle=20, pert_min_scale=0.9, pert_max_scale=1.2, pert_hflip=True, pert_vflip=True, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) if not FLAGS.mixin is None: assert os.path.exists(FLAGS.mixin) picpac_config['mixin'] = FLAGS.mixin picpac_config['mixin_group_delta'] = 1 pass # do we want to apply below to validation images? if not FLAGS.resize_width is None: config['resize_width'] = FLAGS.resize_width if not FLAGS.resize_height is None: config['resize_height'] = FLAGS.resize_height if not FLAGS.max_size is None: config['max_size'] = FLAGS.max_size # load training db assert FLAGS.db and os.path.exists(FLAGS.db) stream = picpac.ImageStream(FLAGS.db, perturb=True, loop=True, **picpac_config) # load validation db val_stream = None if FLAGS.val_db: assert os.path.exists(FLAGS.val_db) val_stream = picpac.ImageStream(FLAGS.val_db, perturb=False, loop=False, **picpac_config) config = tf.ConfigProto() config.gpu_options.allow_growth=True with tf.Session(config=config) as sess: sess.run(init) if FLAGS.resume: saver.restore(sess, FLAGS.resume) step = 0 epoch = 0 global_start_time = time.time() while step < FLAGS.max_steps: start_time = time.time() avg = np.array([0] * len(metrics), dtype=np.float32) for _ in tqdm(range(FLAGS.epoch_steps), leave=False): images, labels, _ = stream.next() feed_dict = {X: images, Y: labels, is_training: True} mm, _, = sess.run([metrics, train_op, ], feed_dict=feed_dict) avg += np.array(mm) pass step += FLAGS.epoch_steps avg /= FLAGS.epoch_steps stop_time = time.time() txt = ', '.join(['%s=%.4f' % (a, b) for a, b in zip(metric_names, list(avg))]) print('step %d: elapsed=%.4f time=%.4f, %s' % (step, (stop_time - global_start_time), (stop_time - start_time), txt)) epoch += 1 # validation if epoch and (epoch % FLAGS.val_epochs == 0) and not val_stream is None: # evaluation val_stream.reset() avg = np.array([0] * len(metrics), dtype=np.float32) C = 0 for images, labels, _ in val_stream: feed_dict = {X: images, Y: labels, is_training: False} mm = sess.run(metrics, feed_dict=feed_dict) avg += np.array(mm) C += 1 pass avg /= C txt = ', '.join(['%s=%.4f' % (a, b) for a, b in zip(metric_names, list(avg))]) print('step %d, validation: %s' % (step, txt)) # model saving if epoch and (epoch % FLAGS.ckpt_epochs == 0): ckpt_path = '%s/%d' % (FLAGS.model, step) saver.save(sess, ckpt_path) print('step %d, saving to %s.' % (step, ckpt_path)) pass pass pass
def main(_): X = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels), name="images") is_training = tf.placeholder(tf.bool, name="is_training") model = Model(X, is_training, FLAGS.model, 'xxx') config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: model.loader(sess) if FLAGS.input: assert False ''' assert os.path.exists(FLAGS.input) image = cv2.imread(FLAGS.input, cv2.IMREAD_COLOR) batch = np.expand_dims(image, axis=0).astype(dtype=np.float32) boxes, probs = sess.run([model.boxes, model.probs], feed_dict={X: batch, is_training: False}) save_prediction_image(FLAGS.input + '.prob.png', image, boxes, probs) ''' if FLAGS.input_db: assert os.path.exists(FLAGS.input_db) from gallery import Gallery picpac_config = { "db": FLAGS.input_db, "loop": False, "shuffle": False, "reshuffle": False, "annotate": False, "channels": FLAGS.channels, "colorspace": "RGB", "stratify": False, "dtype": "float32", "batch": 1, "annotate": [1], "transforms": [ { "type": "resize", "max_size": FLAGS.max_size }, { "type": "clip", "round": FLAGS.backbone_stride }, { "type": "keypoints.basic", 'downsize': 1, 'classes': 1, 'radius': 25 }, { "type": "drop" }, # remove original annotation ] } stream = picpac.ImageStream(picpac_config) gal = Gallery('out') C = 0 for meta, images, _, label, _ in stream: shape = list(images.shape) shape[1] //= FLAGS.stride shape[2] //= FLAGS.stride shape[3] = 1 prob, offsets = sess.run([model.prob, model.offsets], feed_dict={ X: images, is_training: False }) kp = cpp.predict_basic_keypoints(prob[0], offsets[0], FLAGS.stride, 0.1) print(images.shape, prob.shape, offsets.shape, kp) save_prediction_image(gal.next(), images[0], kp, label[0, :, :, 0], prob[0, :, :, 0]) C += 1 if FLAGS.max and C >= FLAGS.max: break pass pass gal.flush() pass
def create_picpac_stream(db_path, is_training): assert os.path.exists(db_path) augments = [] if is_training and FLAGS.augments: with open(FLAGS.augments, 'r') as f: augments = json.loads(f.read()) print("Using augments:") print(json.dumps(augments)) pass print("CACHE:", FLAGS.cache) statinfo = os.stat(db_path) if statinfo.st_size > 0x40000000 and FLAGS.cache: print_red( "DB is probably too big too be cached, consider adding --cache 0") picpac_config = { "db": db_path, "loop": is_training, "shuffle": is_training, "reshuffle": is_training, "annotate": [1], "channels": FLAGS.channels, "stratify": is_training, "dtype": "float32", "batch": FLAGS.batch, "colorspace": "RGB", "cache": FLAGS.cache, "transforms": augments + [ { "type": "resize", "max_size": FLAGS.max_size }, { "type": "clip", "round": FLAGS.backbone_stride }, { "type": "anchors.dense.box", 'downsize': FLAGS.anchor_stride }, { "type": "box_feature" }, { "type": "rasterize" }, ] } if is_training and not FLAGS.mixin is None: #print("mixin support is incomplete in new picpac.") assert os.path.exists(FLAGS.mixin) picpac_config['mixin'] = FLAGS.mixin picpac_config['mixin_group_reset'] = 0 picpac_config['mixin_group_delta'] = 1 return picpac.ImageStream(picpac_config)
def run_training(): try: os.makedirs(FLAGS.model) except: pass config = dict( seed=1996, shuffle=True, reshuffle=True, #resize_width=FLAGS.resize, #resize_height=FLAGS.resize, batch=FLAGS.batch, split=FLAGS.split, split_fold=FLAGS.split_fold, channels=FLAGS.channels, stratify=True, #mixin="db0", #mixin_group_delta=0, pert_color1=10, pert_color2=10, pert_color3=10, #pert_angle=10, pert_min_scale=0.8, pert_max_scale=1.2, #pad=False, #pert_hflip=True, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) # training stream tr_stream = picpac.ImageStream(FLAGS.db, split_negate=False, perturb=False, loop=True, **config) te_stream = None if FLAGS.test_steps > 0: # testing stream, "negate" inverts the image selection specified by split & split_fold # so different images are used for training and testing if FLAGS.test_db: if FLAGS.split > 1: print( "Cannot use cross-validation & evaluation db at the same time" ) print("If --test-db is specified, do not set --split") raise Exception("bad parameters") te_stream = picpac.ImageStream(FLAGS.test_db, perturb=False, loop=False, **config) elif FLAGS.split > 1: te_stream = picpac.ImageStream(FLAGS.db, split_negate=True, perturb=False, loop=False, **config) pass pass with tf.Graph().as_default(): X = tf.placeholder(tf.float32, shape=(FLAGS.batch, None, None, FLAGS.channels), name="images") Y_ = tf.placeholder(tf.float32, shape=(FLAGS.batch, ), name="labels") logits = inference(X, FLAGS.classes) loss, accuracy = fcn_loss(logits, Y_) train_op = training(loss, FLAGS.learning_rate) #summary_op = tf.merge_all_summaries() #summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, tf.get_default_graph()) init = tf.global_variables_initializer() #graph_txt = tf.get_default_graph().as_graph_def().SerializeToString() #with open(os.path.join(FLAGS.train_dir, "graph"), "w") as f: # f.write(graph_txt) # pass saver = tf.train.Saver() config = tf.ConfigProto() config.gpu_options.allow_growth = True #run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) #run_metadata = tf.RunMetadata() loss_sum = 0 accuracy_sum = 0 batch_sum = 0 with tf.Session(config=config) as sess: sess.run(init) for step in xrange(FLAGS.max_steps): images, labels, pad = tr_stream.next() #print(images.shape, labels.shape) feed_dict = {X: images, Y_: labels} #l_v, s_v = sess.run([logits, score], feed_dict=feed_dict) #print(images.shape, s_v.shape, l_v.shape) #_, loss_value = sess.run([train_op, loss], feed_dict=feed_dict, options=run_options, run_metadata=run_metadata) _, loss_value, accuracy_value, ll = sess.run( [train_op, loss, accuracy, logits], feed_dict=feed_dict) #print('XXX', labels[0], accuracy_value, ll[0]) loss_sum += loss_value * FLAGS.batch accuracy_sum += accuracy_value * FLAGS.batch batch_sum += FLAGS.batch if (step + 1) % 1000 == 0: #tl = timeline.Timeline(run_metadata.step_stats) #ctf = tl.generate_chrome_trace_format() #with open('timeline.json', 'w') as f: # f.write(ctf) print(datetime.datetime.now()) print('step %d: loss = %.4f, accuracy = %.4f' % (step + 1, loss_sum / batch_sum, accuracy_sum / batch_sum)) loss_sum = 0 accuracy_sum = 0 batch_sum = 0 #summary_str = sess.run(summary_op, feed_dict=feed_dict) #summary_writer.add_summary(summary_str, step) #summary_writer.flush() if te_stream and (step + 1) % FLAGS.test_steps == 0: # evaluation te_stream.reset() cmatrix = np.zeros((FLAGS.classes, FLAGS.classes)) total = 0 batch_sum2 = 0 loss_sum2 = 0 accuracy_sum2 = 0 for images, labels, pad in te_stream: bs = FLAGS.batch - pad total += 1 if pad > 0: numpy.resize(images, (bs, ) + images.shape[1:]) numpy.resize(labels, (bs, )) feed_dict = {X: images, Y_: labels} loss_value, accuracy_value, ll = sess.run( [loss, accuracy, logits], feed_dict=feed_dict) batch_sum2 += bs cmatrix[int(labels), np.where(ll == np.max(ll))[1]] += np.divide( float(1), np.size(np.where(ll == np.max(ll))[1])) loss_sum2 += loss_value * bs #print(int(labels)) #print(cmatrix) #print(np.size(np.where(ll == np.max(ll))[1])) #print(ll) accuracy_sum2 += accuracy_value * bs pass rowsum = np.sum(cmatrix, axis=1) colsum = np.sum(cmatrix, axis=0) #print(total) #print(np.tile(rowsum,(FLAGS.classes,1)).transpose()) #print(np.tile(colsum,(FLAGS.classes,1))) print('row---label; colum ---predict') print(total) print('evaluation: loss = %.4f, accuracy = %.4f' % (loss_sum2 / batch_sum2, accuracy_sum2 / batch_sum2)) print('accuracy from confusion matrix = %.4f' % np.divide(np.trace(cmatrix), float(total))) print('absolute confusion matrix:') print(cmatrix) #print('confusion matrix divided by total:') #print(np.divide(cmatrix,float(total))) print('confusion matrix divided by col sum:') print(np.divide(cmatrix, np.tile(colsum, (5, 1)))) print('confusion matrix divided by row sum:') print( np.divide(cmatrix, np.tile(rowsum, (5, 1)).transpose())) print('Weighted kappa quadratic scores:') print(quadratic_kappa(cmatrix)) if (step + 1) % FLAGS.save_steps == 0 or ( step + 1) == FLAGS.max_steps: ckpt_path = '%s/%d' % (FLAGS.model, (step + 1)) saver.save(sess, ckpt_path) pass pass pass pass
def main(_): X = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels)) model = Model(X, FLAGS.model, 'xxx') config = tf.ConfigProto() config.gpu_options.allow_growth = True stream = picpac.ImageStream({ 'db': 'scratch/val.db', 'cache': False, 'loop': False, 'channels': FLAGS.channels, 'shuffle': False, 'batch': 1, 'raw': [1], 'colorspace': 'RGB', 'transforms': [ { "type": "resize", "max_size": FLAGS.max_size }, { "type": "clip", "width": FLAGS.fix_size, "height": FLAGS.fix_size }, ] }) with tf.Session(config=config) as sess: model.loader(sess) lookup = {} C = 0 for meta, batch in tqdm(stream, total=stream.size()): path = meta.raw[0][0].decode('ascii') probs = sess.run(model.probs, feed_dict={X: batch}) case = '/'.join(path.split('/')[:-1]) + '/' if not case in lookup: lookup[case] = [0, np.zeros((14, ), dtype=np.float32)] pass case = lookup[case] case[0] = case[0] + 1 case[1] += probs[0] C += 1 if FLAGS.max > 0 and C >= FLAGS.max: break pass with open('predict.csv', 'w') as f, \ open('predict.csv.full', 'w') as f2: for k, v in lookup.items(): probs = np.reshape(v[1] / v[0], (7, 2)) prob = np.sum(probs[:, 1]) if prob > 0.5: l = 1 else: l = 0 f.write('%s,%d\n' % (k, l)) f2.write('%s,%g\n' % (k, prob)) pass pass
def run_training (): seed = 1996 config = dict(seed=seed, loop=True, shuffle=True, reshuffle=True, #resize_width=256, #resize_height=256, batch=1, split=1, split_fold=0, annotate='json', channels=FLAGS.channels, stratify=False, #mixin="db0", #mixin_group_delta=0, #pert_color1=10, #pert_angle=5, #pert_min_scale=0.8, #pert_max_scale=1.2, #pad=False, #pert_hflip=True, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) db='db' tr_stream = picpac.ImageStream(db, negate=False, perturb=True, **config) with tf.Graph().as_default(): X = tf.placeholder(tf.float32, shape=(BATCH, None, None, FLAGS.channels), name="images") Y_ = tf.placeholder(tf.int32, shape=(BATCH, None, None, 1), name="labels") logits, score, params = inference(X) loss = fcn_loss(logits, Y_) train_op = training(loss, FLAGS.learning_rate) summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, tf.get_default_graph()) init = tf.initialize_all_variables() graph_txt = tf.get_default_graph().as_graph_def().SerializeToString() with open(os.path.join(FLAGS.train_dir, "graph"), "w") as f: f.write(graph_txt) pass saver = tf.train.Saver() with tf.Session() as sess: sess.run(init) for step in xrange(FLAGS.max_steps): images, labels, pad = tr_stream.next() #print(images.shape, labels.shape) feed_dict = {X: images, Y_: labels} #l_v, s_v = sess.run([logits, score], feed_dict=feed_dict) #print(images.shape, s_v.shape, l_v.shape) _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict) if step % 100 == 0: print('step %d: loss = %.4f' % (step, loss_value)) summary_str = sess.run(summary_op, feed_dict=feed_dict) summary_writer.add_summary(summary_str, step) summary_writer.flush() if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, os.path.join(FLAGS.train_dir, "model"), global_step=step) pass pass pass pass
def run_training(): config = dict( seed=1996, shuffle=True, reshuffle=True, resize_width=FLAGS.resize, resize_height=FLAGS.resize, batch=FLAGS.batch, split=FLAGS.split, split_fold=FLAGS.split_fold, channels=FLAGS.channels, stratify=True, pert_color1=10, pert_color2=10, pert_color3=10, pert_angle=10, pert_min_scale=0.8, pert_max_scale=1.2, #pad=False, #pert_hflip=True, channel_first=False # this is tensorflow specific # Caffe's dimension order is different. ) # training stream tr_stream = picpac.ImageStream(FLAGS.db, split_negate=False, perturb=True, loop=True, **config) te_stream = None if FLAGS.test_steps > 0: # testing stream, "negate" inverts the image selection specified by split & split_fold # so different images are used for training and testing if FLAGS.test_db: if FLAGS.split > 1: print( "Cannot use cross-validation & evaluation db at the same time" ) print("If --test-db is specified, do not set --split") raise Exception("bad parameters") te_stream = picpac.ImageStream(FLAGS.test_db, perturb=False, loop=False, **config) elif FLAGS.split > 1: te_stream = picpac.ImageStream(FLAGS.db, split_negate=True, perturb=False, loop=False, **config) pass pass with tf.Graph().as_default(): shape = (FLAGS.batch, FLAGS.resize, FLAGS.resize / FLAGS.branches, FLAGS.channels) Xs = [ tf.placeholder(tf.float32, shape=shape, name="input%d" % i) for i in range(FLAGS.branches) ] Y = tf.placeholder(tf.float32, shape=(FLAGS.batch, ), name="labels") if FLAGS.disable: logits = multipod( [X for i, X in enumerate(Xs) if i != FLAGS.disable], FLAGS.classes) else: logits = multipod(Xs, FLAGS.classes) loss, accuracy = fcn_loss(logits, Y) train_op = training(loss, FLAGS.learning_rate) init = tf.global_variables_initializer() saver = tf.train.Saver() config = tf.ConfigProto() config.gpu_options.allow_growth = True loss_sum = 0 accuracy_sum = 0 batch_sum = 0 with tf.Session(config=config) as sess: sess.run(init) for step in xrange(FLAGS.max_steps): images, labels, pad = tr_stream.next() #print(images.shape, labels.shape) feed_dict = make_feed_dict(Xs, Y, images, labels) _, loss_value, accuracy_value = sess.run( [train_op, loss, accuracy], feed_dict=feed_dict) loss_sum += loss_value * FLAGS.batch accuracy_sum += accuracy_value * FLAGS.batch batch_sum += FLAGS.batch if step % 100 == 0: #tl = timeline.Timeline(run_metadata.step_stats) #ctf = tl.generate_chrome_trace_format() #with open('timeline.json', 'w') as f: # f.write(ctf) print(datetime.datetime.now()) print( 'step %d: loss = %.4f, accuracy = %.4f' % (step, loss_sum / batch_sum, accuracy_sum / batch_sum)) loss_sum = 0 accuracy_sum = 0 batch_sum = 0 if te_stream and step % FLAGS.test_steps == 0: # evaluation te_stream.reset() batch_sum2 = 0 loss_sum2 = 0 accuracy_sum2 = 0 for images, labels, pad in te_stream: bs = FLAGS.batch - pad if pad > 0: numpy.resize(images, (bs, ) + images.shape[1:]) numpy.resize(labels, (bs, )) feed_dict = make_feed_dict(Xs, Y, images, labels) _, loss_value, accuracy_value = sess.run( [train_op, loss, accuracy], feed_dict=feed_dict) batch_sum2 += bs loss_sum2 += loss_value * bs accuracy_sum2 += accuracy_value * bs pass print('evaluation: loss = %.4f, accuracy = %.4f' % (loss_sum2 / batch_sum2, accuracy_sum2 / batch_sum2)) if (step + 1) % FLAGS.save_steps == 0 or ( step + 1) == FLAGS.max_steps: saver.save(sess, os.path.join(FLAGS.train_dir, "model"), global_step=step) pass pass pass pass