def experiment2(annotation_file, detector, outputs, exp_name): def getdata(transforms): dataset = Dataset(annotation_file, transforms) return dataset transforms = [[], [FlipImage()], [BlurImage(1)], [RescaleImage(2.0)], [RescaleImage(0.5)], [RotateImage(-90)], [RotateImage(45)]] rows = 2 columns = 4 fig = plt.figure() axes = [] for i, transform in enumerate(transforms): dataset = getdata(transform) img = dataset[3]["image"] preds = detector(img) img = img * 255 img = np.array(img.transpose(1, 2, 0), dtype=np.uint8) print(f"Annotating Image {exp_name} {i}") img = plot_boxes(img, preds) img.save(f"{outputs}/{exp_name}_{i}.jpg") axes.append(fig.add_subplot(rows, columns, i + 1)) axes[-1].set_title(f"Subplot {i+1}") plt.imshow(img) fig.tight_layout() plt.savefig(f"{outputs}/{exp_name}.jpg")
def main(): detector = ObjectDetectionModel() experiment('./data/annotations.jsonl', detector, [ FlipImage("horizontal"), BlurImage(3), RescaleImage(2.0), RescaleImage(0.5), RotateImage(270), RotateImage(45) ], "./output_imgs") # Sample arguments to call experiment()
def main(): detector = ObjectDetectionModel() experiment('./data/annotations.jsonl', detector, [FlipImage(), BlurImage()], None) # Sample arguments to call experiment()
def experiment(annotation_file, detector, transforms, outputs): ''' Function to perform the desired experiments Arguments: annotation_file: Path to annotation file detector: The object detector transforms: List of transformation classes outputs: path of the output folder to store the images ''' #create op_directory if not created if (not os.path.isdir(outputs)): os.mkdir(outputs) if (not outputs.endswith('/')): outputs += '/' all_imgs = os.path.join(outputs, 'All Images') if (not os.path.isdir(all_imgs)): os.mkdir(all_imgs) if (not all_imgs.endswith('/')): all_imgs += '/' #Create the instance of the dataset. dataset = Dataset(annotation_file, []) #Iterate over all data items. j = 0 print('Getting the Predictions...') for data in tqdm(dataset): shp = data['image'].shape img = np.uint8(np.transpose(data['image'], (1, 2, 0)) * 255) img = Image.fromarray(img) #Get the predictions from the detector. boxes = detector(data['image']) gt_boxes = [] for i in range(len(boxes[0])): coord, category, score = boxes[0][i], boxes[1][i], boxes[2][i] gt_boxes.append( [category, coord[0][0], coord[0][1], coord[1][0], coord[1][1]]) #Draw the boxes on the image and save them. op_img = show_boxes(img, gt_boxes) svg_path = all_imgs + str(j) + '.png' op_img.save(svg_path) j += 1 #Do the required analysis experiments. print('Doing the Analysis Part...') img = Image.fromarray( np.uint8(np.transpose(dataset[1]['image'], (1, 2, 0)) * 255)) #original image numbered 1 horiz_flip = FlipImage()(img) blurred = BlurImage()(img) twice_rescaled = RescaleImage((img.size[0] * 2, img.size[1] * 2))(img) half_rescaled = RescaleImage( (floor(img.size[0] * 0.5), floor(img.size[1] * 0.5)))(img) rot_45 = RotateImage(45)(img) rot_90 = RotateImage(90)(img) ip_imgl = [ img, horiz_flip, blurred, twice_rescaled, half_rescaled, rot_45, rot_90 ] op_imgl = [] for c_img in tqdm(ip_imgl): boxes = detector(np.transpose(np.array(c_img), (2, 1, 0)) / 255) # detect boxes gt_boxes = [] for i in range(len(boxes[0])): coord, category, score = boxes[0][i], boxes[1][i], boxes[2][i] gt_boxes.append( [category, coord[0][0], coord[0][1], coord[1][0], coord[1][1]]) op_imgl.append(show_boxes(c_img, gt_boxes)) #add to output image list fg, axes = plt.subplots(nrows=3, ncols=3, figsize=(10, 10)) k = 0 for i in range(3): for j in range(3): axes[i, j].imshow(op_imgl[k]) k += 1 if (k == len(op_imgl)): break if (k == len(op_imgl)): break plt.subplots_adjust(wspace=0, hspace=0) plt.show() analysis_dir = os.path.join(outputs, 'Analysis') if (not analysis_dir.endswith('/')): analysis_dir += '/' if (not os.path.isdir(analysis_dir)): os.mkdir(analysis_dir) i = 0 for c_img in op_imgl: c_img.save(analysis_dir + str(i) + '.png') i += 1
def experiment(annotation_file, detector, transforms, outputs): ''' Function to perform the desired experiments Arguments: annotation_file: Path to annotation file detector: The object detector transforms: List of transformation classes outputs: path of the output folder to store the images ''' #Create the instance of the dataset. d = Dataset(annotation_file, transforms) #Iterate over all data items. for i in range(len(d)): image, image_dict = d[i] #Get the predictions from the detector. pred_boxes, pred_class, pred_score = detector( np.transpose(np.array(image), (2, 0, 1)) / 255) if len(pred_score) > 5: # top 5 confident predictions pred_score = pred_score[:5] pred_boxes = pred_boxes[:5] pred_class = pred_class[:5] image_dict['bboxes'] = [] for k in range(len(pred_score)): my_dict = {} my_dict['bbox'] = list(chain.from_iterable(pred_boxes[k])) for j in range(len(my_dict['bbox'])): my_dict['bbox'][j] = int(my_dict['bbox'][j]) my_dict['category'] = pred_class[k] image_dict['bboxes'].append(my_dict) #Draw the boxes on the image and save them. image = plot_boxes(location='./output/', image_dict=image_dict, image=image, saver=1) #Do the required analysis experiments. my_image = Image.open('./data/imgs/9.jpg') x, y = my_image.size # no transformation d = Dataset(annotation_file, transforms=[]) my_image, my_image_dict = d[9] plt.subplot(2, 4, 1) plt.title('Original Image') plt.imshow(predictor(my_image, my_image_dict, detector)) # horizontal flipping d = Dataset(annotation_file, transforms=[FlipImage()]) image, image_dict = d[9] plt.subplot(2, 4, 2) plt.title('Horizontally Flipped Image') plt.imshow(predictor(image, image_dict, detector)) # blurring d = Dataset(annotation_file, transforms=[BlurImage(radius=3)]) image, image_dict = d[9] plt.subplot(2, 4, 3) plt.title('Blurred Image (Radius = 3)') plt.imshow(predictor(image, image_dict, detector)) # 2x rescaling d = Dataset(annotation_file, transforms=[RescaleImage((2 * y, 2 * x))]) image, image_dict = d[9] plt.subplot(2, 4, 4) plt.title('2x Rescaled Image') plt.imshow(predictor(image, image_dict, detector)) # 0.5x rescaling d = Dataset(annotation_file, transforms=[RescaleImage((y // 2, x // 2))]) image, image_dict = d[9] plt.subplot(2, 4, 5) plt.title('0.5x Rescaled Image') plt.imshow(predictor(image, image_dict, detector)) # 90 degree rotation to right d = Dataset(annotation_file, transforms=[RotateImage(360 - 90)]) image, image_dict = d[9] plt.subplot(2, 4, 6) plt.title('90 degree Rotated Image') plt.imshow(predictor(image, image_dict, detector)) # 45 degree rotation to left d = Dataset(annotation_file, transforms=[RotateImage(45)]) image, image_dict = d[9] plt.subplot(2, 4, 7) plt.title('-45 degree Rotated Image') plt.imshow(predictor(image, image_dict, detector)) plt.show()
def experiment(annotation_file, detector, transforms = None, outputs = None): ''' Function to perform the desired experiments Arguments: annotation_file: Path to annotation file detector: The object detector transforms: List of transformation classes outputs: path of the output folder to store the images ''' #Create the instance of the dataset. d = Dataset(annotation_file, transforms) #Iterate over all data items. for i in range(len(d)): #Get the predictions from the detector. pred = detector(d[i]['image']) #Draw the boxes on the image and save them. plot_boxes(outputs+str(i)+".png",d[i]['image'],pred) #Do the required analysis experiments. fp = "./outputs/" img1 = d[1]['image'] pred = detector(img1) n_img = plot_boxes(fp + "original.png", img1, pred) plt.subplot(2,4,1) plt.imshow(n_img) plt.title("Original") d = Dataset(annotation_file,[FlipImage('horizontal')]) img2 = d[1]['image'] pred = detector(img2) n_img = plot_boxes(fp + "flip.png", img2, pred) plt.subplot(2,4,2) plt.imshow(n_img) plt.title("Horizontally flip") d = Dataset(annotation_file,[BlurImage(3)]) img3 = d[1]['image'] pred = detector(img3) n_img = plot_boxes(fp+"blur.png", img3, pred) plt.subplot(2,4,3) plt.imshow(n_img) plt.title("Blurred") d = Dataset(annotation_file,[RescaleImage((2*img3.shape[2],2*img3.shape[1]))]) img4 = d[1]['image'] pred = detector(img4) n_img = plot_boxes(fp + "2x.png", img4, pred) plt.subplot(2,4,4) plt.imshow(n_img) plt.title("2X") d = Dataset(annotation_file,[RescaleImage((round(0.5*img3.shape[2]),round(0.5*img3.shape[1])))]) img5 = d[1]['image'] pred = detector(img5) n_img = plot_boxes(fp + "halfx.png", img5, pred) plt.subplot(2,4,5) plt.imshow(n_img) plt.title("0.5X") d = Dataset(annotation_file,[RotateImage(90)]) img6 = d[1]['image'] pred = detector(img6) n_img = plot_boxes(fp + "90right.png", img6, pred) plt.subplot(2,4,6) plt.imshow(n_img) plt.title("90degree right") d = Dataset(annotation_file,[RotateImage(-45)]) img7 = d[1]['image'] pred = detector(img7) n_img = plot_boxes(fp + "45left.png", img7, pred) plt.subplot(2,4,7) plt.imshow(n_img) plt.title("45degree left") plt.show()
def experiment(annotation_file, detector, transforms, outputs): ''' Function to perform the desired experiments Arguments: annotation_file: Path to annotation file detector: The object detector transforms: List of transformation classes outputs: path of the output folder to store the images ''' #Create the instance of the dataset. data_set = Dataset(annotation_file, []) #Iterate over all data items. l = len(data_set) for i in range(len(data_set)): img = data_set[i]["image"] var = detector(img) plot_boxes(img, var, outputs + str(i) + ".png") #Get the predictions from the detector. #done above #Draw the boxes on the image and save them. #done above #Do the required analysis experiments. an_op_filepath = "analytics_output/" img = data_set[9]['image'] var = detector(img) plot_boxes(img, var, an_op_filepath + "original.png") data_set = Dataset(annotation_file, [FlipImage('horizontal')]) img = data_set[9]['image'] var = detector(img) plot_boxes(img, var, an_op_filepath + "flip_horizontal.png") data_set = Dataset(annotation_file, [BlurImage(2)]) img = data_set[9]['image'] var = detector(img) plot_boxes(img, var, an_op_filepath + "blurred.png") data_set = Dataset(annotation_file, [RescaleImage([2 * img.shape[2], 2 * img.shape[1]])]) img = data_set[9]['image'] var = detector(img) plot_boxes(img, var, an_op_filepath + "twicex.png") shape = [round(0.5 * img.shape[2]), round(0.5 * img.shape[1])] data_set = Dataset(annotation_file, [RescaleImage(shape)]) img = data_set[9]['image'] var = detector(img) plot_boxes(img, var, an_op_filepath + "halfx.png") data_set = Dataset(annotation_file, [RotateImage(270)]) img = data_set[9]['image'] var = detector(img) plot_boxes(img, var, an_op_filepath + "90degreeright.png") data_set = Dataset(annotation_file, [RotateImage(45)]) img = data_set[9]['image'] var = detector(img) plot_boxes(img, var, an_op_filepath + "45degreeleft.png")