def normalise_image_size_and_bbox(): create_dir(NORMALISED_IMAGES_PATH) create_dir(NORMALISED_BBOX_IMAGES_PATH) LOGGER.debug('Normalising image sizes to same size') fp = open(MIN_DIM, 'rb') dim = pickle.load(fp) LOGGER.debug('Normalising all images to %d x %d', dim, dim) bbox_json = read_json_file(BBOX_JSON_PATH) norm_bbox_json = {} for image in bbox_json.keys(): img = cv2.imread(RAW_TRAIN_IMAGES_PATH + image) y_ = img.shape[0] x_ = img.shape[1] img = cv2.resize(img, (dim, dim)) cv2.imwrite(NORMALISED_IMAGES_PATH + image, img) x_scale = dim / x_ y_scale = dim / y_ tc_x = int(np.round(bbox_json[image][0][0] * x_scale)) tc_y = int(np.round(bbox_json[image][0][1] * y_scale)) bl_x = int(np.round(bbox_json[image][1][0] * x_scale)) bl_y = int(np.round(bbox_json[image][1][1] * y_scale)) norm_bbox_json[image] = [tc_x, tc_y, bl_x, bl_y] im_bbox = cv2.rectangle(img, (tc_x, tc_y), (bl_x, bl_y), (255, 0, 0), 2) cv2.imwrite(NORMALISED_BBOX_IMAGES_PATH + image, im_bbox) write_json_to_file(norm_bbox_json, NORM_BBOX_JSON_PATH)
def write_bbox_csv(): json_bbox = read_json_file(BBOX_JSON_PATH) with open(BBOX_CSV_PATH, encoding='utf8', mode='w') as file: file.write('img_path, xmin, ymin, xmax, ymax, label\n') for key, value in json_bbox.items(): file.write(key + ',' + str(value[0][0]) + ',' + str(value[0][1]) + ',' + str(value[1][0]) + ',' + str(value[1][1]) + ', table\n')
def get_norm_xywh_format(): json_bbox = read_json_file(NORM_BBOX_JSON_PATH) xywh_bbox = dict() for key, value in json_bbox.items(): xywh_bbox[key] = [[(value[2] + value[0]) // 2, (value[3] + value[1]) // 2, value[2] - value[0], value[3] - value[1]]] write_json_to_file(xywh_bbox, BBOX_XYWH_JSON_PATH)
def create_bbox(): LOGGER.debug('Creating Bounded Boxes') json_bbox = read_json_file(BBOX_JSON_PATH) for key, value in json_bbox.items(): im_rd = cv2.imread(RAW_TRAIN_IMAGES_PATH + key) im_bbox = cv2.rectangle(im_rd, (value[0][0], value[0][1]), (value[1][0], value[1][1]), (255, 0, 0), 2) cv2.imwrite(BBOX_IMAGES_PATH + key, im_bbox) LOGGER.debug('Created all bounding boxes for %d images', len(json_bbox.keys())) LOGGER.debug('Images with bounding boxes saved to %s', BBOX_IMAGES_PATH)
def model_train(self, epoch_offset=0): create_dir(MODEL_SAVE_PATH) loss_for_regression = MSELoss() img_coors_json = read_json_file(BBOX_XYWH_JSON_PATH) optimizer = RMSprop(self.parameters(), lr=LEARNING_RATE, momentum=MOMENTUM) # optimizer = Adam(self.parameters(), lr=LEARNING_RATE) # optimizer = SGD(self.parameters(), lr=LEARNING_RATE, momentum=MOMENTUM) scheduler = StepLR(optimizer, step_size=SCHEDULER_STEP, gamma=SCHEDULER_GAMMA) for epoch in range(EPOCHS): epoch_loss = 0.0 scheduler.step(epoch) LOGGER.debug('Epoch: %s, Current Learning Rate: %s', str(epoch + epoch_offset), str(scheduler.get_lr())) for image, coors in img_coors_json.items(): path_of_image = NORMALISED_IMAGES_PATH + image path_of_image = path_of_image.replace('%', '_') img = cv2.imread(path_of_image) img = torch.tensor(img).float().permute(2, 0, 1).unsqueeze(0) img = img.to(self.device) predicted_width, predicted_height, predicted_midpoint = self.forward( img) #all are scaled mp_x = coors[0][0] mp_y = coors[0][1] mp = torch.cat((torch.tensor([[mp_x]]).to( self.device), torch.tensor([[mp_y]]).to(self.device)), dim=1).float() w = coors[0][2] h = coors[0][3] loss1 = loss_for_regression( predicted_height, torch.tensor([[h]]).float().to(self.device)) loss2 = loss_for_regression( predicted_width, torch.tensor([[w]]).float().to(self.device)) loss3 = loss_for_regression(predicted_midpoint, mp.to(self.device)) loss = loss1 + loss2 + loss3 / 2 optimizer.zero_grad() loss.backward() clip_grad_norm(self.parameters(), 0.5) optimizer.step() epoch_loss = epoch_loss + loss.item() if epoch % 5 == 0: print('epoch: ' + str(epoch) + ' ' + 'loss: ' + str(epoch_loss)) if epoch % EPOCH_SAVE_INTERVAL == 0: print('saving') torch.save( self.state_dict(), MODEL_SAVE_PATH + 'model_epc_' + str(epoch + epoch_offset) + '.pt') torch.save( self.state_dict(), MODEL_SAVE_PATH + 'model_epc_' + str(epoch + epoch_offset) + '.pt')
""" Project: Initial setup for Data Processing methods. Author: Goel, Ayush Date: 1st August 2019 """ from os import listdir from utilities import read_json_file, create_dir, LOGGER, MIN_DIM from utilities.config import PROCESSED_DATA_DIR, BBOX_IMAGES_PATH, RAW_TRAIN_IMAGES_PATH, IMG_INFO_JSON_PATH, \ NORMALISED_IMAGES_PATH, NORMALISED_BBOX_IMAGES_PATH IMG_INFO_JSON = read_json_file(IMG_INFO_JSON_PATH) ANNOTATIONS = IMG_INFO_JSON['annotations'] IMAGES = IMG_INFO_JSON['images'] IMAGES_IN_DIR = listdir(RAW_TRAIN_IMAGES_PATH) create_dir(PROCESSED_DATA_DIR)
def model_train(self, epoch_offset=0, lamda=10, nreg=2400, ncls=256): LOGGER.info('Started Training with an offset of %s', str(epoch_offset)) create_dir(MODEL_SAVE_PATH) optimizer = SGD(self.parameters(), lr=LEARNING_RATE, momentum=MOMENTUM) scheduler = StepLR(optimizer, step_size=SCHEDULER_STEP, gamma=SCHEDULER_GAMMA) LOGGER.info( 'Learning Rate: %s, Momentum: %s, Scheduler_step: %s, scheduler_gamma: %s', str(LEARNING_RATE), str(MOMENTUM), str(SCHEDULER_STEP), str(SCHEDULER_GAMMA)) loss_for_classification = NLLLoss() loss_for_regression = SmoothL1Loss() img_coors_json = read_json_file(BBOX_XYWH_JSON_PATH) anchor_box = AnchorBox() all_background_index = [] all_foreground_index = [] all_reg_tensor = [] for image, coors in img_coors_json.items(): li_fore_index, li_back_index, reg_ten_actual = \ anchor_box.calculate_p_for_each_anchor_box(anchor_box.anchor_boxes, coors) all_background_index.append(li_back_index) all_foreground_index.append(li_fore_index) all_reg_tensor.append(reg_ten_actual) for epoch in range(EPOCHS): epoch_loss = 0.0 scheduler.step(epoch) LOGGER.debug('Epoch: %s, Current Learning Rate: %s', str(epoch + epoch_offset), str(scheduler.get_lr())) count = 0 for image, coors in img_coors_json.items(): img = cv2.imread(NORMALISED_IMAGES_PATH + image) img = torch.tensor(img).float().permute(2, 0, 1).unsqueeze(0) img = img.to(self.device) pred_cls, pred_reg = self.forward(img) li_foreground_index = all_foreground_index[count] li_background_index = all_background_index[count] reg_tensor_actual = all_reg_tensor[count] count = count + 1 exp_torch_fg_bg = [] pred_torch_fg = torch.zeros(1, pred_cls.shape[2]) pred_torch_fg = pred_torch_fg.to(self.device) pred_torch_reg = torch.zeros(1, pred_reg.shape[2]) pred_torch_reg = pred_torch_reg.to(self.device) for idx_foreground in li_foreground_index: exp_torch_fg_bg.append(1) pred_torch_fg = torch.cat( (pred_torch_fg, pred_cls[0][idx_foreground].unsqueeze(0)), dim=0) pred_torch_reg = torch.cat( (pred_torch_reg, pred_reg[0][idx_foreground].unsqueeze(0)), dim=0) pred_torch_fg = pred_torch_fg[1:] pred_torch_reg = pred_torch_reg[1:] pred_torch_bg = torch.zeros(1, pred_cls.shape[2]) pred_torch_bg = pred_torch_bg.to(self.device) for idx_background in li_background_index: exp_torch_fg_bg.append(0) pred_torch_bg = torch.cat( (pred_torch_bg, pred_cls[0][idx_background].unsqueeze(0)), dim=0) pred_torch_bg = pred_torch_bg[1:] pred_cls_only_background_foreground = torch.cat( (pred_torch_fg, pred_torch_bg), dim=0) pred_cls_only_background_foreground = LogSoftmax(dim=1).\ forward(pred_cls_only_background_foreground) exp_torch_fg_bg = torch.tensor(exp_torch_fg_bg) exp_torch_fg_bg = exp_torch_fg_bg.to(self.device) pred_cls_only_background_foreground = pred_cls_only_background_foreground.to( self.device) reg_tensor_actual = reg_tensor_actual.to(self.device) pred_torch_reg = pred_torch_reg.to(self.device) cls_loss = loss_for_classification( pred_cls_only_background_foreground, exp_torch_fg_bg) reg_loss = loss_for_regression(reg_tensor_actual, pred_torch_reg) total_image_loss = (cls_loss / ncls) + (reg_loss * lamda / nreg) total_image_loss = total_image_loss.to(self.device) optimizer.zero_grad() total_image_loss.backward() optimizer.step() epoch_loss = epoch_loss + total_image_loss.item() LOGGER.debug('Loss at Epoch %s: %s', str(epoch + epoch_offset), str(epoch_loss)) if epoch % EPOCH_SAVE_INTERVAL == 0: torch.save( self.state_dict(), MODEL_SAVE_PATH + 'model_epc_' + str(epoch + epoch_offset) + '.pt') if epoch % 5 == 0: LOGGER.info('Loss at Epoch %s: %s', str(epoch + epoch_offset), str(epoch_loss))