def main(): new_yolo = constructModel() new_yolo.load_weights("./tiny_yolo_v1.h5", by_name=True, skip_mismatch=True) log_dir = 'log' logging = TensorBoard(log_dir=log_dir) checkpoint = ModelCheckpoint( log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5', monitor='val_loss', save_weights_only=True, save_best_only=True, period=3) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1) early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1) num_train = 460 num = len(new_yolo.layers) - 5 for i in range(num): new_yolo.layers[i].trainable = False new_yolo.compile(optimizer=Adam(lr=1e-3), loss={ 'yolo_loss': lambda y_true, y_pred: y_pred }) new_yolo.fit_generator(data_generator('./train.txt', 1), steps_per_epoch=max(1, num_train // 16), validation_data=data_generator('./vel.txt', 1), validation_steps=1, callbacks=[logging, checkpoint], epochs=50, initial_epoch=0) new_yolo.save_weights(log_dir + 'half_weights.h5') for i in range(len(new_yolo.layers)): new_yolo.layers[i].trainable = True new_yolo.compile(optimizer=Adam(lr=1e-4), loss={ 'yolo_loss': lambda y_true, y_pred: y_pred }) new_yolo.fit_generator( data_generator('./train.txt', 1), steps_per_epoch=max(1, num_train // 16), validation_data=data_generator('./vel.txt', 1), validation_steps=1, callbacks=[logging, checkpoint, reduce_lr, early_stopping], initial_epoch=50, epochs=100) new_yolo.save_weights(log_dir + 'final_weights.h5')
def train(): final_model = traininit() final_model.fit_generator(data_generator(encoding_train, vocab_size, max_len, batch_size=128), samples_per_epoch=samples_per_epoch, nb_epoch=1, verbose=1) final_model.save_weights('time_inceptionV3_3.15_loss.h5') final_model.save_weights('time_inceptionV3_3.21_loss.h5') final_model.save_weights('time_inceptionV3_7_loss_3.2604.h5') return final_model
y2 - y1, linewidth=2, facecolor='none', edgecolor=(i + 1) * np.array(colors[level]) / anchors_per_cell) ax.add_patch(p) # ## Data Generator # # In[13]: # Create data generator random_rois = 2000 g = modellib.data_generator(dataset, config, shuffle=True, random_rois=random_rois, batch_size=4, detection_targets=True) # In[14]: # Uncomment to run the generator through a lot of images # to catch rare errors # for i in range(1000): # print(i) # _, _ = next(g) # In[15]: # Get Next Image if random_rois:
model = baseline_model(eng_vocab_size, ger_vocab_size, eng_length, ger_length, 256) model.summary() checkpoint = create_checkpoint(model_name='baseline_model.h5') epochs = args["epochs"] batch_size = 64 train_steps = len(train) // batch_size val_steps = len(dev) // batch_size train_generator = data_generator(train, eng_tokenizer, eng_length, ger_tokenizer, ger_length, ger_vocab_size, batch_size=batch_size) val_generator = data_generator(dev, eng_tokenizer, eng_length, ger_tokenizer, ger_length, ger_vocab_size, batch_size=batch_size) H = model.fit_generator(train_generator, steps_per_epoch=train_steps, validation_data=val_generator,
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = False model_path = 'checkpoints/deepfashion.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('deepfashion_13.json', 'r')).values() } num_classes = 13 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] #_, model = efficientdet(phi=phi, # weighted_bifpn=weighted_bifpn, # num_classes=num_classes, # score_threshold=score_threshold) models = EfficientDetModel(0) model = models.p_model model.load_weights(model_path, by_name=True) misc_effect = MiscEffectMask() train_dataset = CocoDataset('data/sample_val', ['train', 'val'], misc_effect=misc_effect) train_generator = data_generator(train_dataset, shuffle=True, phi=0, batch_size=1) for images, _, _, masks_batch, boxes_batch, labels_batch in next( train_generator): image = images[0] src_image = image.copy() cv2.imwrite('results/image.jpg', src_image * 255) d_image = image.copy() #image = image[:, :, ::-1] h, w = image.shape[:2] #image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels, _ = model.predict_on_batch([ np.expand_dims(image, axis=0), masks_batch, boxes_batch, labels_batch ]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) # testing.... boxes = norm_boxes_graph(boxes, (512, 512)) print(boxes.shape, boxes_batch[0].shape, masks_batch[0].shape, labels_batch[0].shape) rois, roi_gt_class_ids, target_mask = build_mask_target_graph( boxes, boxes_batch[0], masks_batch[0], labels_batch[0]) target_mask = target_mask.numpy() ''' positive_overlap = 0.4 negative_overlap = 0.3 b_gt_boxes = boxes_batch[0] b_gt_masks = masks_batch[0] gt_class_ids = labels_batch[0] num_classes=13 mask_shape=[28,28] TRAIN_ROIS_PER_IMAGE = 100 boxes = norm_boxes_graph(boxes, (512, 512)) rois, _ = trim_zeros_graph(boxes, name="trim_rois") #print(rois) b_gt_boxes, non_zeros = trim_zeros_graph(b_gt_boxes, name='trim_gt_boxes') b_gt_masks = tf.gather(b_gt_masks, tf.where(non_zeros)[:, 0], axis=0, name="trim_gt_masks") # shape [num_instances, img_size, img_size] #print(b_gt_boxes, non_zeros) #print(b_gt_masks.shape) # skip handle coco crowd for now gt_class_ids = tf.boolean_mask(gt_class_ids, non_zeros, name="trim_gt_class_ids") overlaps = overlaps_graph(rois, b_gt_boxes) #print(overlaps) # Determine positive and negative ROIs roi_iou_max = tf.reduce_max(overlaps, axis=1) # 1. Positive ROIs are those with >= 0.5 IoU with a GT box positive_roi_bool = (roi_iou_max >= positive_overlap) positive_indices = tf.where(positive_roi_bool)[:, 0] # 2. Negative ROIs are those with < 0.5 with every GT box. Skip crowds. negative_indices = tf.where((roi_iou_max < negative_overlap))[:, 0] positive_count = int(TRAIN_ROIS_PER_IMAGE * 0.8) positive_indices = tf.random_shuffle(positive_indices)[:positive_count] positive_count = tf.shape(positive_indices)[0] # Negative ROIs. Add enough to maintain positive:negative ratio. r = 1.0 / 0.8 negative_count = tf.cast(r * tf.cast(positive_count, tf.float32), tf.int32) - positive_count negative_indices = tf.random_shuffle(negative_indices)[:negative_count] positive_overlaps = tf.gather(overlaps, positive_indices) roi_gt_box_assignment = tf.cond( tf.greater(tf.shape(positive_overlaps)[1], 0), true_fn = lambda: tf.argmax(positive_overlaps, axis=1), false_fn = lambda: tf.cast(tf.constant([]),tf.int64) ) #print(positive_indices) #print(roi_gt_box_assignment) # Permute masks to [N, height, width, 1] transposed_masks = tf.expand_dims(b_gt_masks, -1) # Pick the right mask for each ROI roi_masks = tf.gather(transposed_masks, roi_gt_box_assignment) roi_gt_class_ids = tf.gather(gt_class_ids, roi_gt_box_assignment) boxes = tf.gather(rois, positive_indices) box_ids = tf.range(0, tf.shape(roi_masks)[0]) b_masks = tf.image.crop_and_resize(tf.cast(roi_masks, tf.float32), boxes, box_ids, mask_shape) b_masks = tf.round(tf.squeeze(b_masks, axis=-1)) positive_rois = tf.gather(rois, positive_indices) negative_rois = tf.gather(rois, negative_indices) # Append negative ROIs and pad bbox deltas and masks that # are not used for negative ROIs with zeros. N = tf.shape(negative_rois)[0] P = tf.maximum(TRAIN_ROIS_PER_IMAGE - tf.shape(rois)[0], 0) rois = tf.concat([positive_rois, negative_rois], axis=0) rois = tf.pad(rois, [(0, P), (0, 0)]) roi_gt_class_ids = tf.pad(roi_gt_class_ids, [(0, N + P)]) target_mask = tf.pad(b_masks, [[0, N + P], (0, 0), (0, 0)]) target_mask = target_mask.numpy() ''' rois = denorm_boxes_graph(rois, (512, 512)) print("target mask shape", target_mask.shape) print(roi_gt_class_ids) for box in rois: xmin, ymin, xmax, ymax = list(map(int, box)) print(box) cv2.rectangle(d_image, (xmin, ymin), (xmax, ymax), colors[0], 1) #cv2.rectangle(target_mask[0], (xmin, ymin), (xmax, ymax), colors[0], 1) cv2.rectangle(masks_batch[0][0], (xmin, ymin), (xmax, ymax), colors[0], 1) cv2.rectangle(masks_batch[0][1], (xmin, ymin), (xmax, ymax), colors[0], 1) cv2.imwrite('results/detect_img.jpg', d_image * 255) for i in range(target_mask.shape[0]): cv2.imwrite('results/preds/tmask_img_{}.jpg'.format(i), target_mask[i] * 255) cv2.imwrite('results/mask_img1.jpg', masks_batch[0][0] * 255) cv2.imwrite('results/mask_img2.jpg', masks_batch[0][1] * 255) #print(target_mask.shape, target_mask) #cv2.imwrite('results/mask_img2.jpg', masks[1]) #for i in range(10): #cv2.imwrite('results/dmask_img_{}.jpg', tf.round(target_mask[i]) *255) #boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels) #print(time.time() - start) #boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold #indices = np.where(scores[:] > score_threshold)[0] # select those detections #boxes = boxes[indices] #labels = labels[indices] #draw_boxes(src_image, boxes, scores, labels, colors, classes) #cv2.namedWindow('image', cv2.WINDOW_NORMAL) #cv2.imwrite('results/image.jpg', src_image) break
model_path = args.model_path assert mode in ['train', 'evaluate'] print('\n') print('{:30}{}'.format('mode: ', mode)) print('{:30}{}'.format('dataset: ', data_path)) print('{:30}{}'.format('model_path: ', model_path)) # configure config = Configurations() # for traning if mode == 'train': # training data train_data_path = os.path.join(data_path, 'train') train_generator = data_generator(train_data_path, config) # validation data val_data_path = os.path.join(data_path, 'validation') val_generator = data_generator(val_data_path, config) # creat model model = Dogsandcats(mode, config) # train model.train(train_generator, val_generator) # for evaluatint else: # test data test_data_path = os.path.join(data_path, 'test')
annotations, prob=self.translate_prob, border_value=self.border_value) #print("pass translate") #print(image.shape, annotations['masks'][0].shape) return image, annotations if __name__ == '__main__': from generators.coco_ins import CocoDataset from model import data_generator misc_effect = MiscEffectMask() train_dataset = CocoDataset('data/sample_val', ['train', 'val'], misc_effect=misc_effect) train_generator = data_generator(train_dataset, shuffle=True, phi=0, batch_size=1) for i in range(train_generator.size()): image = train_generator.load_image(i) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) annotations = train_generator.load_annotations(i) boxes = annotations['bboxes'].astype(np.int32) #quadrangles = annotations['quadrangles'].astype(np.int32) for box in boxes: cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 1) #cv2.drawContours(image, quadrangles, -1, (0, 255, 255), 1) src_image = image.copy() # cv2.namedWindow('src_image', cv2.WINDOW_NORMAL) #cv2.imshow('src_image', src_image)
filename = 'Flicker8k_text/Flickr_8k.trainImages.txt' train = load_set(filename) print('Dataset: %d' % len(train)) # descriptions train_descriptions = load_clean_descriptions('descriptions.txt', train) print('Descriptions: train=%d' % len(train_descriptions)) # photo features train_features = load_photo_features('features.pkl', train) print('Photos: train=%d' % len(train_features)) # prepare tokenizer tokenizer = load(open('tokenizer.pkl', 'rb')) vocab_size = len(tokenizer.word_index) + 1 print('Vocabulary Size: %d' % vocab_size) # determine the maximum sequence length max_length = max_length(train_descriptions) print('Description Length: %d' % max_length) # define the model model = define_model(vocab_size, max_length) # train the model, run epochs manually and save after each epoch epochs = 20 steps = len(train_descriptions) for i in range(epochs): # create the data generator generator = data_generator( train_descriptions, train_features, tokenizer, max_length, vocab_size) # fit for one epoch model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1) # save model model.save('model/model_' + str(i) + '.h5')