def test_pretrained(self): # Load efficientdet pretrained on VOC2007 model = EfficientDet.from_pretrained('D0-VOC', score_threshold=.6) print('Done loading...') image = io.load_image('test/data/VOC2007/JPEGImages/000002.jpg', (model.config.input_size, ) * 2) n_image = normalize_image(image) n_image = tf.expand_dims(n_image, axis=0) classes = voc.IDX_2_LABEL boxes, labels, scores = model(n_image, training=False) labels = [classes[l] for l in labels[0]] im = image.numpy() for l, box, s in zip(labels, boxes[0].numpy(), scores[0]): x1, y1, x2, y2 = box.astype('int32') cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(im, l + ' {:.2f}'.format(s), (x1, y1 - 10), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2) plt.imshow(im) plt.axis('off') plt.savefig('test.png') plt.show(block=True)
def test_keras_pretrained(self): # Load efficientdet pretrained on VOC2007 model = EfficientDet(D=0, num_classes=20, weights='D0-VOC', score_threshold=.4) print('Done loading...') image = io.load_image('imgs/cat-dog.jpg', model.config.input_size) n_image = normalize_image(image) n_image = tf.expand_dims(n_image, axis=0) classes = voc.IDX_2_LABEL boxes, labels, scores = model(n_image, training=False) labels = [classes[l] for l in labels[0]] colors = visualizer.colors_per_labels(labels) im = visualizer.draw_boxes(image, boxes[0], labels, scores[0], colors=colors) plt.imshow(im) plt.axis('off') plt.savefig('test.png') plt.show(block=True)
from efficientdet import EfficientDet import tensorflow as tf from PIL import Image import numpy as np import time import cv2 gpus = tf.config.experimental.list_physical_devices(device_type='GPU') for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) efficientdet = EfficientDet() capture = cv2.VideoCapture(0) # capture=cv2.VideoCapture("1.mp4") fps = 0.0 while (True): t1 = time.time() # 读取某一帧 ref, frame = capture.read() # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转变成Image frame = Image.fromarray(np.uint8(frame)) # 进行检测 frame = np.array(efficientdet.detect_image(frame)) # RGBtoBGR满足opencv显示格式 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) fps = (fps + (1. / (time.time() - t1))) / 2 print("fps= %.2f" % (fps))
from efficientdet import EfficientDet from PIL import Image import tensorflow as tf gpus = tf.config.experimental.list_physical_devices(device_type='GPU') for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) efficientdet = EfficientDet() # while True: # img = "./VOCdevkit_test/VOC2007/JPEGImages/000008.jpg" # try: # image = Image.open(img) # except: # print('Open Error! Try again!') # else: # r_image = efficientdet.detect_image(image) # r_image.save("img/000008.jpg") test_img_files = open('./pascal_voc_testing_data.txt') test_img_dir = './VOCdevkit_test/VOC2007/JPEGImages/' test_images = [] for line in test_img_files: line = line.strip() ss = line.split(' ') test_images.append(ss[0]) output_file = open('./test_prediction.txt', 'w') for img_name in test_images: img = test_img_dir + img_name
#-------------------------------------# # 对单张图片进行预测 #-------------------------------------# from efficientdet import EfficientDet from PIL import Image efficientdet = EfficientDet() while True: img = input('Input image filename:') try: image = Image.open(img) except: print('Open Error! Try again!') continue else: r_image = efficientdet.detect_image(image) r_image.show()
#-------------------------------------# # 对单张图片进行预测 #-------------------------------------# from efficientdet import EfficientDet from PIL import Image efficientdet = EfficientDet( './logs/Epoch29-Total_Loss0.7971-Val_Loss0.7432.pth') while True: img = input('Input image filename:') try: image = Image.open(img) except: print('Open Error! Try again!') continue else: r_image = efficientdet.detect_image(image) r_image.show()
from keras.layers import Input from efficientdet import EfficientDet from PIL import Image import numpy as np import time import cv2 efficientdet = EfficientDet() # 调用摄像头 capture=cv2.VideoCapture(0) # capture=cv2.VideoCapture("1.mp4") fps = 0.0 while(True): t1 = time.time() # 读取某一帧 ref,frame=capture.read() # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) # 转变成Image frame = Image.fromarray(np.uint8(frame)) # 进行检测 frame = np.array(efficientdet.detect_image(frame)) # RGBtoBGR满足opencv显示格式 frame = cv2.cvtColor(frame,cv2.COLOR_RGB2BGR) fps = ( fps + (1./(time.time()-t1)) ) / 2 print("fps= %.2f"%(fps)) frame = cv2.putText(frame, "fps= %.2f"%(fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
help='do you have a gpu?', default=True) parser.add_argument('-n', "--num2show", type=int, help='num img 2 show', default=1) parser.add_argument('-r', "--root", type=str, help='root dir filled with *.jpg') parser.add_argument('-i', "--filename", type=str, help='filename', default='') args = parser.parse_args() efficientdet = EfficientDet(args.model_path, args.version, args.conf, args.cuda) if args.num2show == 1: image = Image.open(os.path.join(args.root, args.filename)) res, cls, score = efficientdet.detect_image(image) print(cls, score) # r_image.show() else: print('结果将会保存到temp.png') files = os.listdir(args.root) idx = [ int(len(os.listdir(args.root)) * random.random()) for i in range(args.num2show) ] imgs = [Image.open(os.path.join(args.root, files[id])) for id in idx]
from efficientdet import EfficientDet from efficientdet.data import preprocess from efficientdet.utils.io import load_image from efficientdet.data.voc import IDX_2_LABEL # import efficientdet print(hub.__version__) print(tf.__version__) model_url = "https://tfhub.dev/tensorflow/efficientdet/d3/1" # base_model = hub.KerasLayer(model_url, input_shape=(299, 299, 3)) # base_model = hub.load(model_url) # print(base_model.summary()) """my_model = tf.keras.applications.VGG16() print(my_model.summary()) """ """hub_layer = hub.KerasLayer(model_url, output_shape=[20], input_shape=[], dtype=tf.string, trainable=True) model = tf.keras.Sequential() model.add(hub_layer) model.add(tf.keras.layers.Dense(16, activation="relu", input_shape=[20])) model.add(tf.keras.layers.Dense(1, activation="sigmoid")) print(model.summary())""" """bla = tarfile.open("efficientdet-d0.tar.gz") bla.extractall() print(bla) print("done")""" model = EfficientDet.from_pretrained("D0-VOC", score_threshold=0.3) image_size = model.config.input_size print(image_size)
from efficientdet import EfficientDet from PIL import Image efficientdet = EfficientDet() while True: img = input('Input image filename:') try: image = Image.open(img) except: print('Open Error! Try again!') continue else: r_image = efficientdet.detect_image(image) r_image.show() efficientdet.close_session()
# In[ ]: from efficientdet import visualizer from efficientdet import EfficientDet from efficientdet.data import preprocess from efficientdet.utils.io import load_image from efficientdet.data.voc import IDX_2_LABEL import tensorflow as tf # In[3]: model = EfficientDet.from_pretrained('D0-VOC', score_threshold=.3) image_size = model.config.input_size # In[4]: image = load_image('sample.jpg', image_size) image.shape # In[ ]: n_image = preprocess.normalize_image(image) n_image = tf.expand_dims(n_image, 0)
from efficientdet.utils import postprocess import matplotlib.pyplot as plt from torchvision import transforms from dataset.augmentation import MaxSizeResizer, ToTensor, SquarePad, Augmenter parser = argparse.ArgumentParser() parser.add_argument('--input', default='data/letters/my/-11_issues.png') parser.add_argument('--output', default='output.jpg') parser.add_argument('--weights', default='model/letters-nandos-efficientdet-d0-e3000.pth') parser.add_argument('--network', default='efficientdet-d0') parser.add_argument('--num_classes', default=1) args = parser.parse_args() if __name__ == '__main__': model = EfficientDet(args.num_classes, network=args.network) model.load_state_dict(torch.load(args.weights)) model.eval() image = cv2.imread(args.input) transforms = transforms.Compose( [ MaxSizeResizer(1280), SquarePad(), ToTensor(), ] ) image = transforms({'image': image})['image'].float() image = image.view(1, *image.size()) classes_, _, train_rects, output_rects = model(image.float()) out_classes, out_rects = postprocess(classes_[0], output_rects[0], 0.15)
#----------------------------------------------------# # 对视频中的predict.py进行了修改, # 将单张图片预测、摄像头检测和FPS测试功能 # 整合到了一个py文件中,通过指定mode进行模式的修改。 #----------------------------------------------------# import time import cv2 import numpy as np from PIL import Image from efficientdet import EfficientDet if __name__ == "__main__": efficientdet = EfficientDet() #-------------------------------------------------------------------------# # mode用于指定测试的模式: # 'predict'表示单张图片预测 # 'video'表示视频检测 # 'fps'表示测试fps #-------------------------------------------------------------------------# mode = "predict" #-------------------------------------------------------------------------# # video_path用于指定视频的路径,当video_path=0时表示检测摄像头 # video_save_path表示视频保存的路径,当video_save_path=""时表示不保存 # video_fps用于保存的视频的fps # video_path、video_save_path和video_fps仅在mode='video'时有效 # 保存视频时需要ctrl+c退出才会完成完整的保存步骤,不可直接结束程序。 #-------------------------------------------------------------------------# video_path = 0 video_save_path = ""
MaxSizeResizer(1280), SquarePad(), ToTensor(), ])) elif args.dataset == 'letters': train_dataset = LetterDataset(args.image_dir, args.label_path, device=device, transform=transforms.Compose([ MaxSizeResizer(1536), SquarePad(), ToTensor(), ])) model = EfficientDet(train_dataset.num_classes(), network=args.network, device=device).to(device) model.eval() init_epoch = 0 if args.checkpoint: checkpoint_name = os.path.basename(args.checkpoint) init_epoch = int(re.findall(r'e\d+', checkpoint_name)[0][1:]) model.load_state_dict(torch.load(args.checkpoint)) model.train() optimizer = ranger(model.parameters(), 1e-3) for e in range(init_epoch, args.epochs): losses = [] for images, rects, classes in DataLoader(train_dataset, 1, False):