def check_on_train_clip(video_id, weights, suffix, is_test=False): if 'resnet' in weights: model = build_resnet(input_shape) else: model = build_model(input_shape) pool = ThreadPool(processes=8) model.compile(loss=MultiboxLoss(NUM_CLASSES, neg_pos_ratio=2.0, pos_cost_multiplier=1.0).compute_loss, optimizer=Adam(lr=1e-3)) model.load_weights(weights) model.summary() priors = priors_from_model(model) bbox_util = BBoxUtility(NUM_CLASSES, priors) dataset = SSDDataset(bbox_util=bbox_util, is_test=is_test) outdir = '../output/predictions_ssd/' + video_id + suffix os.makedirs(outdir, exist_ok=True) batch_size = 4 frame_id = 0 for x_batch, frames in dataset.generate_x_for_train_video_id(video_id=video_id, batch_size=batch_size, pool=pool): predictions = model.predict(x_batch) results = bbox_util.detection_out(predictions) for batch_id in range(predictions.shape[0]): print(results[batch_id]) display_img_with_rects(img=utils.preprocessed_input_to_img_resnet(x_batch[batch_id]) * 255, results=results, res_idx=batch_id) plt.savefig('{}/{:04}.jpg'.format(outdir, frame_id+1)) plt.clf() frame_id += 1 print(frame_id)
def check(fold, weights): dataset = ClassificationDataset(fold=fold) model = build_model_densenet_161() model.load_weights(weights) batch_size = 2 for x_batch, y_batch in dataset.generate(batch_size=batch_size): print(y_batch) predicted = model.predict_on_batch(x_batch) print(predicted) for i in range(batch_size): plt.imshow(utils.preprocessed_input_to_img_resnet(x_batch[i])) true_species = y_batch['cat_species'][i] true_cover = y_batch['cat_cover'][i] predicted_species = predicted[0][i] predicted_cover = predicted[1][i] for cls_id, cls in enumerate(SPECIES_CLASSES): print('{:12} {:.02f} {:.02f}'.format( cls, true_species[cls_id], predicted_species[cls_id])) for cls_id, cls in enumerate(COVER_CLASSES): print('{:12} {:.02f} {:.02f}'.format(cls, true_cover[cls_id], predicted_cover[cls_id])) print(SPECIES_CLASSES[np.argmax(y_batch['cat_species'][i])], COVER_CLASSES[np.argmax(y_batch['cat_cover'][i])]) plt.show()
def check(weights): model = build_model(input_shape) model.compile(loss=MultiboxLoss(NUM_CLASSES, neg_pos_ratio=2.0, pos_cost_multiplier=1.0).compute_loss, optimizer=Adam(lr=1e-3)) model.load_weights(weights) model.summary() priors = priors_from_model(model) bbox_util = BBoxUtility(NUM_CLASSES, priors) dataset = SSDDataset(bbox_util=bbox_util) for x_batch, y_batch in dataset.generate_ssd(batch_size=4, is_training=False, verbose=True, always_shuffle=True): predictions = model.predict(x_batch) results = bbox_util.detection_out(predictions) for batch_id in range(4): display_img_with_rects(img=utils.preprocessed_input_to_img_resnet(x_batch[batch_id])*255, results=results, res_idx=batch_id) plt.show()
def check_model(model_name, weights, fold): model = MODELS[model_name].factory(lock_base_model=True) model.load_weights(weights, by_name=True) dataset = SingleFrameCNNDataset( preprocess_input_func=MODELS[model_name].preprocess_input, batch_size=1, validation_batch_size=1, fold=fold) batch_id = 0 for X, y in dataset.generate_test(): pred = model.predict_on_batch(X) print() for i, cls in enumerate(CLASSES): print(f'gt: {y[0, i]} pred: {pred[0, i]:.03f} {cls}') batch_id += 1 for batch_frame in range(dataset.batch_size): plt.imshow(utils.preprocessed_input_to_img_resnet(X[batch_frame])) # plt.imshow(X[batch_frame]/2.0+0.5) plt.show()
def check_generator(use_test): dataset = SingleFrameCNNDataset( preprocess_input_func=preprocess_input_resnet50, batch_size=2, validation_batch_size=2, fold=1) batch_id = 0 startTime = time.time() if use_test: gen = dataset.generate_test() else: gen = dataset.generate() for X, y in gen: batch_id += 1 elapsedTime = time.time() - startTime startTime = time.time() print(f'{batch_id} {elapsedTime:.3}') for batch_frame in range(dataset.batch_size): print(y[batch_frame]) plt.imshow(utils.preprocessed_input_to_img_resnet(X[batch_frame])) plt.show()
def unprocess_input(x): return utils.preprocessed_input_to_img_resnet(x)