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 with tf.Session(config=config) as sess: model.loader(sess) gal_0 = Gallery('gallery_0', ext='png') gal_1 = Gallery('gallery_1', ext='png') gal_2 = Gallery('gallery_2', ext='png') for img in glob(os.path.join(FLAGS.image, "*/*.jpg")): filename = img.split("/")[-1] image = cv2.imread(img, cv2.IMREAD_COLOR) batch = np.expand_dims(image, axis=0).astype(dtype=np.float32) probs = sess.run(model.probs, feed_dict={X: batch}) cls = np.argmax(probs[0]) if cls == 0: cv2.imwrite(gal_0.next(filename=filename), image) if cls == 1: cv2.imwrite(gal_1.next(filename=filename), image) if cls == 2: cv2.imwrite(gal_2.next(filename=filename), image) ''' if cls == 1: cv2.imwrite('gallery_1/'+filename,image) gal_1.next(filename=filename) if cls == 2: cv2.imwrite('gallery_2/'+filename,image) gal_2.next(filename=filename) ''' gal_0.flush() gal_1.flush() gal_2.flush()
def write_dicom_volume_html_flip(volume, path, title): gal = Gallery('pethtmlview/' + path, score=False, title=title) for i in range(volume.shape[0]): transposedImage = cv2.transpose(volume[i]) flippedImage = cv2.flip(transposedImage, 1) cv2.imwrite(gal.next(), flippedImage) pass gal.flush()
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 write_dicom_volume_html_resize_twice(volume, path, title): gal = Gallery('pethtmlview/' + path, score=False, title=title) for i in range(volume.shape[0]): resizedImage = cv2.resize(volume[i], (0, 0), fx=2.0, fy=2.0, interpolation=cv2.INTER_NEAREST) cv2.imwrite(gal.next(), resizedImage) pass gal.flush()
def write_dicom_volume_html_flip_resize(volume, path, title): gal = Gallery('pethtmlview/' + path, score=False, title=title) for i in range(volume.shape[0]): transposedImage = cv2.transpose(volume[i]) flippedImage = cv2.flip(transposedImage, 1) resizedImage = cv2.resize(flippedImage, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST) cv2.imwrite(gal.next(), resizedImage) pass gal.flush()
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 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(_): cc = 0 with Model(FLAGS.model, name=FLAGS.name) as model: for out, inp in DS: gal = Gallery(out, ext='.gif') for root, dirs, files in os.walk(inp, topdown=False): for path in files: image = cv2.imread(os.path.join(root, path), cv2.IMREAD_GRAYSCALE) image = image.astype(np.float32) image = cv2.resize(image, None, fx=FLAGS.rx, fy=FLAGS.rx) prob = model.apply( np.expand_dims(np.expand_dims(image, axis=0), axis=3))[0] visualize(gal.next(), image, prob) gal.flush() pass
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(_): 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 main (_): global PIXEL_MEANS logging.basicConfig(filename='train-%s-%s.log' % (FLAGS.net, datetime.datetime.now().strftime('%Y%m%d-%H%M%S')),level=logging.DEBUG, format='%(asctime)s %(message)s') if FLAGS.model: 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.int32, shape=(None, None, None, 1), name="labels") is_training = tf.placeholder(tf.bool, name="is_training") #with \ # slim.arg_scope([slim.batch_norm], decay=0.9, epsilon=5e-4): with slim.arg_scope([slim.conv2d, slim.conv2d_transpose, slim.max_pool2d], padding='SAME'), \ slim.arg_scope([slim.conv2d, slim.conv2d_transpose], weights_regularizer=slim.l2_regularizer(2.5e-4), normalizer_fn=slim.batch_norm, normalizer_params={'decay': 0.9, 'epsilon': 5e-4, 'scale': False, 'is_training':is_training}), \ slim.arg_scope([slim.batch_norm], is_training=is_training): logits, FLAGS.stride = getattr(nets, FLAGS.net)(X-PIXEL_MEANS) # probability of class 1 -- not very useful if FLAGS.classes > 2 #probs = tf.squeeze(tf.slice(tf.nn.softmax(logits), [0,0,0,1], [-1,-1,-1,1]), 3) probs = tf.squeeze(tf.slice(tf.nn.softmax(logits), [0,0,0,1], [-1,-1,-1,1]), 1) loss, metrics = fcn_loss(logits, Y) metric_names = [x.name[:-2] for x in metrics] def format_metrics (avg): return ' '.join(['%s=%.3f' % (a, b) for a, b in zip(metric_names, list(avg))]) global_step = tf.train.create_global_step() LR = tf.train.exponential_decay(FLAGS.lr, global_step, FLAGS.decay_steps, FLAGS.decay_rate, staircase=True) if FLAGS.adam: print("Using Adam optimizer, reducing LR by 100x") optimizer = tf.train.AdamOptimizer(LR/100) else: optimizer = tf.train.MomentumOptimizer(learning_rate=LR, momentum=0.9) train_op = slim.learning.create_train_op(loss, optimizer, global_step=global_step) saver = tf.train.Saver(max_to_keep=FLAGS.max_to_keep) stream = create_picpac_stream(FLAGS.db, True) if FLAGS.gallery: gal = Gallery(FLAGS.gallery, ext='.png') C = 0 for _, images, label in stream: image = images[0] print(type(image), image.shape) #, type(anno)) #print(image.dtype, anno.dtype) #print(np.mean(image[:, :, 0]), np.mean(image[:, :, 1]), np.mean(image[:, :, 2])) C += 1 #print('%d / %d' % (C, stream.size())) cv2.imwrite(gal.next(), image) #cv2.imwrite(gal.next(), anno) #cv2.imwrite(gal.next(), label * 255) #cv2.imwrite(gal.next(), draw) if C == FLAGS.gallery_max: break pass gal.flush() sys.exit(0) # load validation db val_stream = None if FLAGS.val_db: val_stream = create_picpac_stream(FLAGS.val_db, False) config = tf.ConfigProto() config.gpu_options.allow_growth=True epoch_steps = FLAGS.epoch_steps if epoch_steps is None: epoch_steps = (stream.size() + FLAGS.batch-1) // FLAGS.batch best = 0 with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) if FLAGS.resume: saver.restore(sess, FLAGS.resume) global_start_time = time.time() epoch = 0 step = 0 while epoch < FLAGS.max_epochs: start_time = time.time() cnt, metrics_sum = 0, np.array([0] * len(metrics), dtype=np.float32) progress = tqdm(range(epoch_steps), leave=False) for _ in progress: _, images, labels = stream.next() #print ("labels:",labels) feed_dict = {X: images, Y: labels, is_training: True} mm, _ = sess.run([metrics, train_op], feed_dict=feed_dict) metrics_sum += np.array(mm) * images.shape[0] cnt += images.shape[0] metrics_txt = format_metrics(metrics_sum/cnt) progress.set_description(metrics_txt) step += 1 pass stop = time.time() msg = 'train epoch=%d step=%d ' % (epoch, step) msg += metrics_txt msg += ' elapsed=%.3f time=%.3f ' % (stop - global_start_time, stop - start_time) print_green(msg) logging.info(msg) epoch += 1 if (epoch % FLAGS.val_epochs == 0) and val_stream: lr = sess.run(LR) # evaluation Ys, Ps = [], [] cnt, metrics_sum = 0, np.array([0] * len(metrics), dtype=np.float32) val_stream.reset() progress = tqdm(val_stream, leave=False) for _, images, labels in progress: feed_dict = {X: images, Y: labels, is_training: False} p, mm = sess.run([probs, metrics], feed_dict=feed_dict) metrics_sum += np.array(mm) * images.shape[0] cnt += images.shape[0] Ys.extend(list(meta.labels)) Ps.extend(list(p)) metrics_txt = format_metrics(metrics_sum/cnt) progress.set_description(metrics_txt) pass assert cnt == val_stream.size() avg = metrics_sum / cnt if avg[0] > best: best = avg[0] msg = 'valid epoch=%d step=%d ' % (epoch-1, step) msg += metrics_txt if FLAGS.classes == 2: # display scikit-learn metrics Ys = np.array(Ys, dtype=np.int32) Ps = np.array(Ps, dtype=np.float32) msg += ' sk_acc=%.3f auc=%.3f' % (accuracy_score(Ys, Ps > 0.5), roc_auc_score(Ys, Ps)) pass msg += ' lr=%.4f best=%.3f' % (lr, best) print_red(msg) logging.info(msg) #log.write('%d\t%s\t%.4f\n' % (epoch, '\t'.join(['%.4f' % x for x in avg]), best)) # model saving if (epoch % FLAGS.ckpt_epochs == 0) and FLAGS.model: ckpt_path = '%s/%d' % (FLAGS.model, epoch) saver.save(sess, ckpt_path) print('saved to %s.' % ckpt_path) pass pass pass
def main(_): setup_params() model = VoxelNet() model.build_graph() saver = tf.train.Saver() config = tf.ConfigProto() config.gpu_options.allow_growth = True is_val = True if is_val: db = FLAGS.val_db columns = 2 # two columns to visualize groundtruth else: db = FLAGS.test_db columns = 1 pass if FLAGS.results: sp.check_call('mkdir -p %s/data' % FLAGS.results, shell=True) with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) if not FLAGS.test_labels: saver.restore(sess, FLAGS.model) gal = Gallery('output', cols=columns) C = 0 with open(db, 'r') as f: for l in f: pk = int(l.strip()) sample = Sample(pk, LOAD_IMAGE2 | LOAD_VELO | LOAD_LABEL2, is_training=is_val) points = sample.get_voxelnet_points() points, mask, index = model.vxl.voxelize_points([points], T) feed_dict = { model.is_training: False, model.points: points, model.mask: mask, model.index: index } if is_val: # for validation set, produce the ground-truth boxes boxes_gt = sample.get_voxelnet_boxes(["Car"]) #for row in boxes_gt: # print(row) if FLAGS.test_labels: # 2 lines below are for testing the C++ code probs, _, params, _ = model.vxl.voxelize_labels( [boxes_gt], np.array(model.priors, dtype=np.float32), FLAGS.rpn_stride, FLAGS.lower_th, FLAGS.upper_th) sample.load_voxelnet_boxes(boxes_gt, 'Car') # visualize groundtruth labels image3d = np.copy(sample.image2) for box in sample.label2: draw_box3d(image3d, box, sample.calib) pass cv2.imwrite(gal.next(), image3d) if not FLAGS.test_labels: probs, params = sess.run([model.probs, model.params], feed_dict=feed_dict) boxes = model.vxl.generate_boxes( probs, params, np.array(model.priors, dtype=np.float32), FLAGS.anchor_th) boxes = cpp.nms(boxes, FLAGS.nms_th) boxes = boxes[0] #print("++++") #for row in boxes: # print(row) #print('====') print(np.max(probs), len(boxes)) sample.load_voxelnet_boxes(boxes, 'Car') if FLAGS.results: save_label2('%s/data/%06d.txt' % (FLAGS.results, pk), sample.label2) image3d = np.copy(sample.image2) for box in sample.label2: draw_box3d(image3d, box, sample.calib) pass cv2.imwrite(gal.next(), image3d) C += 1 if C >= FLAGS.max: break pass pass gal.flush() pass pass
def main (argv): nodule_model = Model(FLAGS.prob, FLAGS.mode, FLAGS.fts, FLAGS.channels, FLAGS.prob_dropout, FLAGS.fts_dropout) with open(os.path.join('models', FLAGS.score), 'rb') as f: score_model = pickle.load(f) case = FsCase(FLAGS.input) case.normalizeHU() case = case.rescale3D(SPACING) lung, _ = mesh.segment_lung(case.images) save_mesh(lung, os.path.join(FLAGS.output, 'lung')) mask = mesh.convex_hull(lung) #body, _ = mesh.segment_body(case.images) #save_mesh(body, os.path.join(FLAGS.output, 'body')) case.standardize_color() case.round_stride(FLAGS.stride) mask = case.copy_replace_images(mask) mask.round_stride(FLAGS.stride) mask = mask.images views = [case.transpose(AXIAL), case.transpose(SAGITTAL), case.transpose(CORONAL)] if FLAGS.dilate > 0: ksize = FLAGS.dilate * 2 + 1 mask = grey_dilation(mask, size=(ksize, ksize, ksize), mode='constant') pass if True: with tf.Session() as sess: tf.global_variables_initializer().run() nodule_model.load(sess) dim, nodules = nodule_model.apply(sess, views, mask) pass pass else: dim = 11 nodules = [] fts = [] pos = [] #print(nodules) fts.append(pyramid(dim, nodules)) # global pos.append(None) # global for nodule in nodules: fts.append(pyramid(dim, [nodule])) pos.append(nodule[3]) pass Nt = np.array(fts, dtype=np.float32) Ny = score_model.predict_proba(Nt)[:,1] global_score = float(Ny[0]) #print('GLOBAL SCORE:', global_score) pw = sorted(zip(pos, list(Ny)), key=lambda x:x[1], reverse=True) gal = Gallery(FLAGS.output, cols=5, header=['nodule','score','axial','sagittal','coronal']) anno = Annotations() C = 1 for box, score in pw: if box is None: continue if score < 0.1: break anno.add(box, str(score)) gal.text('%d' % C) gal.text('%.4f' % score) for v in VIEWS: cc, (y0, x0, y1, x1) = box_center(box, v) view = views[v] image = get3c(view.images, cc) cv2.rectangle(image, (x0,y0), (x1,y1), (0,255,255)) if v == AXIAL: image = cv2.flip(image, 1) elif v == SAGITTAL: image = cv2.transpose(image) image = cv2.flip(image, 0) elif v == CORONAL: image = cv2.flip(image, -1) cv2.imwrite(gal.next(), image) pass C += 1 pass gal.flush('plumo.html', extra={'score':global_score}) Papaya(os.path.join(FLAGS.output, 'papaya'), case, annotations=anno) pass
def papaya(samples=['126823']): gal = Gallery('/output/preview', cols=4) for sample in samples: try: pbb = np.load('grt123-DSB2017/bbox_result/%s_pbb.npy' % sample) pbb = np.asarray(pbb, np.float32) pbb_original = pbb[:] vol = np.load('grt123-DSB2017/prep_result/%s_clean.npy' % sample) meta = pickle.load( open('grt123-DSB2017/prep_result/' + sample + '.pickle', 'rb')) except: continue pbb = nms(pbb, 0.4) ii_list = pbb[:, 0].argsort()[-5:][::-1] extendbox = meta['extendbox'] resolution = meta['resolution'] spacing = meta['spacing'] mask_shape = meta['mask_shape'] vol = vol[0] boxes = [] output_dic = {} anno = Annotations() for index, ii in enumerate(ii_list): p, z, y, x, r = pbb[ii, :] if index == 0: gal.text('case %s' % sample) cv2.imwrite(gal.next(), draw_bb(vol[int(round(z)), :, :], y, x, r)) cv2.imwrite(gal.next(), draw_bb(vol[:, int(round(y)), :], z, x, r)) cv2.imwrite(gal.next(), draw_bb(vol[:, :, int(round(x))], z, y, r)) dicom_z = mask_shape[0] - int( round((z + extendbox[0][0] - r / 2) * resolution[0] / spacing[0])) dicom_y = int( round((y + extendbox[1][0] - r / 2) * resolution[1] / spacing[1])) dicom_x = mask_shape[2] - int( round((x + extendbox[2][0] + r / 2) * resolution[2] / spacing[2])) dicom_r_xy = int(round(r * resolution[1] / spacing[1])) dicom_r_z = int(round(r * resolution[0] / spacing[0])) dicom_z = max(dicom_z, 0) box = [ max(dicom_z - dicom_r_z, 0), dicom_y, dicom_x, dicom_z, dicom_y + dicom_r_xy, dicom_x + dicom_r_xy ] anno.add(box=box, hint='Blob %d: %.3f' % (index + 1, p)) dicom_z_to_save = int( round((z + extendbox[0][0] - r / 2) * resolution[0] / spacing[0])) dicom_y_to_save = int( round((y + extendbox[1][0] - r / 2) * resolution[1] / spacing[1])) dicom_x_to_save = int( round((x + extendbox[2][0] - r / 2) * resolution[2] / spacing[2])) box_to_save = [ dicom_z_to_save, dicom_y_to_save, dicom_x_to_save, dicom_z_to_save + dicom_r_z, dicom_y_to_save + dicom_r_xy, dicom_x_to_save + dicom_r_xy ] output_dic[str(p)] = box_to_save papaya = Papaya('/output/papaya_' + sample, case_path=dicom_path + sample, annotations=anno) pickle.dump(output_dic, open('/output/boxes/%s.pickle' % sample, 'wb')) gal.flush() return
def write_dicom_volume_html (volume, path, title): gal = Gallery('pethtmlview/'+path, score=False, title=title) for i in range(volume.shape[0]): cv2.imwrite(gal.next(), volume[i]) pass gal.flush()
def main (_): assert FLAGS.db and os.path.exists(FLAGS.db) assert FLAGS.model and os.path.exists(FLAGS.model + '.meta') GRAY = tf.placeholder(tf.float32, shape=(None, None, None, 1)) mg = meta_graph.read_meta_graph_file(FLAGS.model + '.meta') COLOR, = tf.import_graph_def(mg.graph_def, name='colorize', #input_map={'L:0':L}, input_map={'gray:0':GRAY}, return_elements=['color: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 gray, _, _ = _pic2pic.encode_bgr(images.copy(), FLAGS.downsize) #l, ab, w = _pic2pic.encode_lab(images.copy(), FLAGS.downsize) # color, = sess.run([COLOR], feed_dict={GRAY: gray}) cv2.imwrite(gallery.next(), gray[0]) full = np.zeros(images.shape, dtype=np.float32) color /= 255.0 gray /= 255.0 _, H, W, _ = images.shape for i in range(images.shape[0]): lab = cv2.cvtColor(cv2.cvtColor(gray[i], cv2.COLOR_GRAY2BGR), cv2.COLOR_BGR2LAB) print(lab.shape) 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]) if FLAGS.s_add and FLAGS.s_mul: hsv = cv2.cvtColor(full[i], cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) s *= FLAGS.s_mul s += FLAGS.s_add hsv = cv2.merge([h, s, v]) cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR, full[i]) pass full *= 255 cv2.imwrite(gallery.next(), full[0]) #y_p = decode_lab(l, ab_p, T=FLAGS.T) c += 1 print('%d/%d' % (c, FLAGS.max)) pass gallery.flush() pass pass
from lung import * from glob import glob from gallery import Gallery gal = Gallery('nodule_samples', cols=3) def visualize(image, y, x, r): image = np.copy(image, order='C') image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX) image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) cv2.circle(image, (int(x), int(y)), int(r + 5), (0, 255, 0)) return image for path in glob('scratch/luna16/*.h5')[:20]: volume = H5Volume(path) for i in range(volume.annotation.shape[0]): print(path, i) sub, anno = extract_nodule(volume, volume.annotation[i], 0.8, [128, 128, 128], random_crop=True) anno = np.round(anno).astype(np.int32) z, y, x, r = anno cv2.imwrite(gal.next(), visualize(sub.images[z, :, :], y, x, r)) cv2.imwrite(gal.next(), visualize(sub.images[:, y, :], z, x, r)) cv2.imwrite(gal.next(), visualize(sub.images[:, :, x], z, y, r)) pass gal.flush()
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=FLAGS.padding): logits, stride = getattr(nets, FLAGS.net)(X) loss, metrics = fcn_loss(logits, Y) prob = logits2prob(logits) #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=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. ) 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() for node in graph_def.node: if 'padding' in node.attr and not 'Backprop' in node.name: padding = node.attr['padding'].s if padding != FLAGS.padding: logging.error( "node %s type %s incorrect padding %s, should be %s" % (node.name, node.op, padding, FLAGS.padding)) pass pass 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): while True: images, labels, _ = tr_stream.next() _, H, W, _ = images.shape if FLAGS.max_size: if max(H, W) > FLAGS.max_size: continue break if FLAGS.padding == 'SAME' and FLAGS.clip: images = clip(images, stride) labels = clip(labels, stride) 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 gal = None if FLAGS.val_plot: gal = Gallery(os.path.join(FLAGS.val_plot, str(step))) 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) pp, mm, = sess.run([prob, metrics], feed_dict=feed_dict) if gal: save_vis(gal.next(), pp, images) avg += np.array(mm) cc += 1 if gal: gal.flush() 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(_): 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