def make_dataset(category=None, dirlist=None, height=32, width=32, channel=3, extensions=None): print("\n** Make " + category) class_len = len(dirlist) io_mode = "w" label_number = 0 if (not (util.check_path(path=PACK_PATH + "/images/"))): util.make_path(path=PACK_PATH + "/images/") util.refresh_directory(PACK_PATH + "/images/dataset/") channel = 1 for di in dirlist: tmp_path = PACK_PATH + "/dataset/" + category + "/" + di fi_list = util.get_filelist(directory=tmp_path, extensions=extensions) cnt = 0 for fi in fi_list: tmp_sub, tmp_file = util.get_dir_and_file_name(path=fi) cnt += 1 image = cv2.imread(fi) resized_image = cv2.resize(image, (width, height)) cvf.save_image(path=PACK_PATH + "/images/dataset/", filename=str(label_number) + "_" + str(cnt) + ".png", image=resized_image) if (channel == 1): resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY) height, width = resized_image.shape else: height, width, channel = resized_image.shape resized_image = resized_image.reshape((height * width * channel)) np.save(file=tmp_path + "/" + tmp_file, arr=resized_image) label_number += 1 if (os.path.exists(PACK_PATH + "/" + category)): # management storage shutil.rmtree(PACK_PATH + "/" + category) f = open(PACK_PATH + "/dataset/format.txt", "w") f.write(str(label_number)) f.write("\n") f.write(str(height * width * channel)) f.write("\n") f.write(str(height)) f.write("\n") f.write(str(width)) f.write("\n") f.write(str(channel)) f.close()
def main(): extensions = ["BMP", "bmp", "PNG", "png", "JPG", "jpg", "JPEG", "jpeg"] util.refresh_directory(PACK_PATH+"/images") print("Enter the path") # usr_path = input(">> ") usr_path = "/media/yeonghyeon/Toshiba/lung/datasets/20171204" if(util.check_path(usr_path)): files = util.get_filelist(directory=usr_path, extensions=extensions) for fi in files: print(fi) tmp_sub, tmp_file = util.get_dir_and_file_name(path=fi) if(not(util.check_path(path=PACK_PATH+"/images/"+str(tmp_file)+"/"))): util.make_path(path=PACK_PATH+"/images/"+str(tmp_file)+"/") image = cvf.load_image(path=fi) if(image.shape[0] > image.shape[1]): # height > width resized = cvf.resizing(image=image, width=int(500*(image.shape[1]/image.shape[0])), height=500) else: resized = cvf.resizing(image=image, width=500, height=int(500*(image.shape[0]/image.shape[1]))) zeropad = cvf.zero_padding(image=resized, height=500, width=500) print(image.shape) print(resized.shape) print(zeropad.shape) cvf.save_image(path=PACK_PATH+"/images/", filename=str(tmp_file)+".png", image=zeropad) else: print("Invalid path :"+usr_path)
def extract_segments(filename): tmp_sub, tmp_file = util.get_dir_and_file_name(path=filename) if (not (util.check_path(path=PACK_PATH + "/images/" + str(tmp_file) + "/"))): util.make_path(path=PACK_PATH + "/images/" + str(tmp_file) + "/") origin = cvf.load_image(path=filename) gray = cvf.rgb2gray(rgb=origin) resized = cvf.resizing(image=gray, width=500) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + ".png", image=resized) mulmul = resized.copy() for i in range(20): ret, thresh = cv2.threshold(mulmul, np.average(mulmul) * 0.3, 255, cv2.THRESH_BINARY) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_thresh1.png", image=thresh) mulmul = cvf.normalizing(binary_img=resized * (thresh / 255)) movavg = cvf.moving_avg_filter(binary_img=mulmul, k_size=10) adap = cvf.adaptiveThresholding(binary_img=movavg, neighbor=111, blur=False, blur_size=3) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_adap.png", image=255 - adap) result = resized * ((255 - adap) / 255) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_result1.png", image=result) movavg = cvf.moving_avg_filter(binary_img=result, k_size=10) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_result2.png", image=movavg) ret, thresh = cv2.threshold(movavg, np.average(movavg) * 0.5, 255, cv2.THRESH_BINARY_INV) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_thresh2.png", image=thresh) contours = cvf.contouring(binary_img=thresh) boxes = cvf.contour2box(contours=contours, padding=20) resized = cvf.resizing(image=gray, width=500) cnt = 0 for box in boxes: x, y, w, h = box if ((x > 0) and (y > 0)): if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])): cvf.save_image( path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_0_" + str(cnt) + ".png", image=thresh[y:y + h, x:x + w]) pad = cvf.zero_padding(image=thresh[y:y + h, x:x + w], height=500, width=500) cvf.save_image( path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_1_" + str(cnt) + ".png", image=pad) pad2 = cvf.remain_only_biggest(binary_img=pad) cvf.save_image( path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_2_" + str(cnt) + ".png", image=pad2) pad_res = cvf.zero_padding(image=resized[y:y + h, x:x + w], height=500, width=500) cvf.save_image( path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_3_" + str(cnt) + ".png", image=pad_res * (pad2 / 255)) cvf.save_image( path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_4_" + str(cnt) + ".png", image=resized[y:y + h, x:x + w]) cnt += 1 for b in boxes: x, y, w, h = b if ((x > 0) and (y > 0)): if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])): cv2.rectangle(resized, (x, y), (x + w, y + h), (255, 255, 255), 2) cv2.rectangle(thresh, (x, y), (x + w, y + h), (255, 255, 255), 2) # cvf.save_image(path=PACK_PATH+"/images/"+str(tmp_file)+"/", filename="opened.png", image=dilated) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename="contour.png", image=thresh) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename="resized.png", image=resized) cvf.save_image(path=PACK_PATH + "/images/", filename="resized" + str(tmp_file) + ".png", image=resized)
def extract_lung(usr_path, extensions=None, height=None, width=None, channel=None, sess=None, x_holder=None, training=None, prediction=None, saver=None): if (not (util.check_path(path=PACK_PATH + "/results/"))): util.make_path(path=PACK_PATH + "/results/") summf = open(PACK_PATH + "/results/summary.csv", "w") summf.write("FILENAME") summf.write(",") summf.write("DETECT") summf.write(",") summf.write("IOU") summf.write("\n") files = util.get_filelist(directory=usr_path, extensions=extensions) files.sort() for filename in files: print(filename) if (util.check_file(filename=filename)): tmp_sub, tmp_file = util.get_dir_and_file_name(path=filename) if (not (util.check_path(path=PACK_PATH + "/results/" + str(tmp_file) + "/"))): util.make_path(path=PACK_PATH + "/results/" + str(tmp_file) + "/") origin = cvf.load_image(path=filename) try: gray = cvf.rgb2gray(rgb=origin) except: # if origin image is grayscale gray = origin resized = cvf.resizing(image=gray, width=500) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_pre1_origin.png", image=resized) mulmul = resized.copy() for i in range(20): ret, thresh = cv2.threshold(mulmul, np.average(mulmul) * 0.3, 255, cv2.THRESH_BINARY) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_pre2_thresh.png", image=thresh) mulmul = cvf.normalizing(binary_img=resized * (thresh / 255)) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_pre3_normalize.png", image=mulmul) movavg = cvf.moving_avg_filter(binary_img=mulmul, k_size=10) adap = cvf.adaptiveThresholding(binary_img=movavg, neighbor=111, blur=False, blur_size=3) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_pre4_adaptrhesh.png", image=255 - adap) masking = resized * ((255 - adap) / 255) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_pre5_mask1.png", image=masking) movavg = cvf.moving_avg_filter(binary_img=masking, k_size=5) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_pre6_mask2.png", image=movavg) ret, thresh = cv2.threshold(movavg, np.average(movavg) * 0.5, 255, cv2.THRESH_BINARY_INV) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_pre7_thresh.png", image=thresh) contours = cvf.contouring(binary_img=thresh) boxes_tmp = cvf.contour2box(contours=contours, padding=20) boxes = cvf.rid_repetition(boxes=boxes_tmp, binary_img=thresh) if (os.path.exists(PACK_PATH + "/checkpoint/checker.index")): saver.restore(sess, PACK_PATH + "/checkpoint/checker") f = open(PACK_PATH + "/dataset/labels.txt", 'r') content = f.readlines() f.close() for idx in range(len(content)): content[idx] = content[idx][:len(content[idx]) - 1] # rid \n boxes_pred = [] cnt = 0 for b in boxes: x, y, w, h = b if ((x > 0) and (y > 0)): if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])): pad = cvf.zero_padding(image=thresh[y:y + h, x:x + w], height=500, width=500) pad2 = cvf.remain_only_biggest(binary_img=pad) pad_res = cvf.zero_padding(image=resized[y:y + h, x:x + w], height=500, width=500) xdata = pad_res * (pad2 / 255) prob = sess.run(prediction, feed_dict={ x_holder: convert_image(image=xdata, height=height, width=width, channel=channel), training: False }) result = str(content[int(np.argmax(prob))]) acc = np.max(prob) boxes_pred.append([x, y, w, h, result, acc]) # cvf.save_image(path=PACK_PATH+"/results/"+str(tmp_file)+"/", filename=str(tmp_file)+"_"+str(result)+"_"+str(int(round(acc, 2)*100))+"_"+str(cnt)+".png", image=xdata) cnt += 1 boxes_pred = sorted(boxes_pred, key=lambda l: l[4], reverse=True) # sort by result boxes_pred = sorted(boxes_pred, key=lambda l: l[5], reverse=True) # sort by acc ratio = origin.shape[0] / resized.shape[0] save_crops(image=resized, boxes=boxes_pred, ratio=1, file_name=tmp_file) concats = concatenate(image=resized, boxes=boxes_pred, ratio=1, file_name=tmp_file) iou, bbox = intersection_over_union(filename=filename, boxes=concats, ratio=ratio) summf.write(str(filename)) summf.write(",") summf.write(str(len(concats))) summf.write(",") summf.write(str(iou)) summf.write("\n") origin_res1 = cvf.resizing(image=origin, width=500) origin_res2 = origin_res1.copy() origin_res3 = origin_res1.copy() origin_res_lr = draw_boxes(image=origin_res1, boxes=boxes_pred, ratio=1, file_name=tmp_file) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_origin_lr.png", image=origin_res_lr) origin_res_concat1 = draw_boxes(image=origin_res1, boxes=concats, ratio=1, file_name=tmp_file) cvf.save_image( path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_origin_lr_and_concat.png", image=origin_res_concat1) origin_res_concat2 = draw_boxes(image=origin_res2, boxes=concats, ratio=1, file_name=tmp_file) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_origin_concat.png", image=origin_res_concat2) if (len(bbox) > 0): origin_res_bbox = draw_boxes(image=origin_res3, boxes=bbox, ratio=1, file_name=tmp_file) cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_origin_bbox.png", image=origin_res_bbox) origin_res_concat3 = draw_boxes(image=origin_res3, boxes=concats, ratio=1, file_name=tmp_file) cvf.save_image( path=PACK_PATH + "/results/" + str(tmp_file) + "/", filename=str(tmp_file) + "_origin_concat_bbox.png", image=origin_res_concat3) else: print("You must training first!") else: print("Invalid File: " + str(filename)) summf.close()
def training_process(sess=None, dataset=None, x=None, y_=None, training=None, train_step=None, accuracy=None, loss=None, saver=None, batch_size=0, steps=0): print("\n** Training process start!") te_am = dataset.test.amount if (batch_size > te_am): batch_size = te_am if (not (util.check_path(PACK_PATH + "/checkpoint"))): util.make_path(PACK_PATH + "/checkpoint") train_acc_list = [] test_acc_list = [] train_loss_list = [] test_loss_list = [] stepper = 1 if (steps >= 1000): stepper = int(steps / 100) print("\nTraining to " + str(steps) + " steps | Batch size: %d\n" % (batch_size)) for i in range(steps): sys.stdout.write("Loading next batch\r") sys.stdout.flush() train_batch = dataset.train.next_batch(batch_size=batch_size) test_batch = dataset.test.next_batch(batch_size=batch_size) sys.stdout.write("Evalueation \r") sys.stdout.flush() train_accuracy = accuracy.eval(feed_dict={ x: train_batch[0], y_: train_batch[1], training: False }) test_accuracy = accuracy.eval(feed_dict={ x: test_batch[0], y_: test_batch[1], training: False }) train_loss = loss.eval(feed_dict={ x: train_batch[0], y_: train_batch[1], training: False }) test_loss = loss.eval(feed_dict={ x: test_batch[0], y_: test_batch[1], training: False }) train_acc_list.append(train_accuracy) test_acc_list.append(test_accuracy) train_loss_list.append(train_loss) test_loss_list.append(test_loss) if (i % stepper == 0): print("step [ %d / %d ]\nAccuracy train: %.5f | test: %.5f" % (i, steps, train_accuracy, test_accuracy)) print("CE loss train: %.5f | test: %.5f" % (train_loss, test_loss)) sys.stdout.write("Training \r") sys.stdout.flush() sess.run(train_step, feed_dict={ x: train_batch[0], y_: train_batch[1], training: True }) saver.save(sess, PACK_PATH + "/checkpoint/checker") test_batch = dataset.test.next_batch(batch_size=batch_size) test_accuracy = accuracy.eval(feed_dict={ x: test_batch[0], y_: test_batch[1], training: False }) test_loss = loss.eval(feed_dict={ x: test_batch[0], y_: test_batch[1], training: False }) print("\nFinal Test accuracy, loss | %.5f\t %.5f\n" % (test_accuracy, test_loss)) util.save_graph_as_image(train_list=train_acc_list, test_list=test_acc_list, ylabel="accuracy") util.save_graph_as_image(train_list=train_loss_list, test_list=test_loss_list, ylabel="loss")
def extract_segments(filename): tmp_sub, tmp_file = util.get_dir_and_file_name(path=filename) if (not (util.check_path(path=PACK_PATH + "/images/" + str(tmp_file)))): util.make_path(path=PACK_PATH + "/images/" + str(tmp_file)) origin = cvf.load_image(path=filename) gray = cvf.rgb2gray(rgb=origin) resized = cvf.resizing(image=gray, width=500) avg = np.average(resized) # feed = cvf.feeding_outside_filter(binary_img=resized, thresh=100) # cvf.save_image(path=PACK_PATH+"/images/"+str(tmp_file)+"/", filename="feed.png", image=feed) movavg = cvf.moving_avg_filter(binary_img=resized, k_size=10) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename="movavg.png", image=movavg) ret, thresh = cv2.threshold(movavg, np.average(movavg) * 0.5, 255, cv2.THRESH_BINARY_INV) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename="origin.png", image=origin) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename="thresh.png", image=thresh) contours = cvf.contouring(binary_img=thresh) boxes = cvf.contour2box(contours=contours, padding=50) resized = cvf.resizing(image=gray, width=500) cnt = 0 for box in boxes: x, y, w, h = box if ((x > 0) and (y > 0)): if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])): cvf.save_image( path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_0_" + str(cnt) + ".png", image=thresh[y:y + h, x:x + w]) pad = cvf.zero_padding(image=thresh[y:y + h, x:x + w], height=500, width=500) cvf.save_image( path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_1_" + str(cnt) + ".png", image=pad) cvf.save_image( path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename=str(tmp_file) + "_2_" + str(cnt) + ".png", image=resized[y:y + h, x:x + w]) cnt += 1 for b in boxes: x, y, w, h = b if ((x > 0) and (y > 0)): if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])): cv2.rectangle(resized, (x, y), (x + w, y + h), (255, 255, 255), 2) cv2.rectangle(thresh, (x, y), (x + w, y + h), (255, 255, 255), 2) # cvf.save_image(path=PACK_PATH+"/images/"+str(tmp_file)+"/", filename="opened.png", image=dilated) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename="contour.png", image=thresh) cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/", filename="resized.png", image=resized) cvf.save_image(path=PACK_PATH + "/images/", filename="resized" + str(tmp_file) + ".png", image=resized)