def Modelmain(): test_set = ClownDataset() test_set.load_dataset( '/home/ubuntu/FobiaPhilter/ActionFiles/FramesFromVideo', is_train=False) test_set.prepare() cfg = PredictionConfig() model_path = '/home/ubuntu/FobiaPhilter/ActionFiles/model/mask_rcnn_clown_cfg_0025.h5' model = MaskRCNN(mode='inference', model_dir='./', config=cfg) model.load_weights(model_path, by_name=True) cfg2 = InferenceConfig() cfg3 = InferenceConfigOrig() weights_path = '/home/ubuntu/FobiaPhilter/ActionFiles/model/mask_rcnn_coco.h5' model2 = MaskRCNN(mode='inference', model_dir='./', config=cfg2) model2.load_weights(weights_path, by_name=True) model3 = MaskRCNN(mode='inference', model_dir='./', config=cfg3) model3.load_weights(weights_path, by_name=True) if phobia == 'clown': plot_predicted_new(test_set, model, model2, cfg, cfg2, class_names, class_names2, n_images) #Export imageID vs. original Filename files = [] for m in test_set.image_from_source_map: files.append(m) df = pd.DataFrame({'Original_files': files}) df['Index_outputFile'] = df.index df['Original_files'] = df['Original_files'].str.replace( 'dataset.image', '').astype('int64') df = df.sort_values(by=['Original_files']) df.to_csv( '/home/ubuntu/FobiaPhilter/ActionFiles/TestSampleImageID.txt') elif phobia != 'clown': plot_predicted_coco(test_set, model3, cfg3, class_names2, n_images, phobia) #Export imageID vs. original Filename files = [] for m in test_set.image_from_source_map: files.append(m) df = pd.DataFrame({'Original_files': files}) df['Index_outputFile'] = df.index df['Original_files'] = df['Original_files'].str.replace( 'dataset.image', '').astype('int64') df = df.sort_values(by=['Original_files']) df.to_csv( '/home/ubuntu/FobiaPhilter/ActionFiles/TestSampleImageID.txt') else: pass
def main(images_dir, ann_file, pred_file, net_weights_file): # test set test_set = OurDataset() test_set.load_dataset(images_dir, ann_file, is_train=True, val_percentage = 0.0) test_set.prepare() print('Test images: %d' % len(test_set.image_ids)) # create config cfg = PredictionConfig() # define the model model = MaskRCNN(mode='inference', model_dir='./', config=cfg) # load model weights model.load_weights(net_weights_file, by_name=True) # generate the annotations for the test set using the model json_output = generateAnnotations(test_set,model,cfg) # save the file with open(pred_file, 'w') as outfile: json.dump(json_output, outfile) print('RESULTS FILE GENERATED') # evalutate the predictions and save them in results.txt evaluation(ann_file,pred_file) print('EVALUATION DONE') print('PROGRAM FINISHED')
def train(config=SeptinConfig()): """Train the model.""" # Training dataset. dataset_train = SeptinDataset() dataset_train.load_Septin("train") dataset_train.prepare() print('Train: %d' % len(dataset_train.image_ids)) # Validation dataset dataset_val = SeptinDataset() dataset_val.load_Septin("test") dataset_val.prepare() print('Test: %d' % len(dataset_val.image_ids)) config = SeptinConfig() config.display() # define the model model = MaskRCNN(mode='training', model_dir='./', config=config) # load weights (mscoco) and exclude the output layers model.load_weights('mask_rcnn_coco.h5', by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) # train weights (output layers or 'heads') model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=8, layers='heads')
def img_preprocess(self, img): """Pre-processes the input image. img: Input image of shape (-1,XX,YY,3) Returns: molded_image: Molded imimg_age to be used as model input image_meta: Input image metadata anchors: [N, (y1, x1, y2, x2)]. All generated anchors in one array. Sorted with the same order of the given scales. So, anchors of scale[0] come first, then anchors of scale[1], and so on. window: (y1, x1, y2, x2). If max_dim is provided, padding might be inserted in the returned image. If so, this window is the coordinates of the image part of the full image (excluding the padding). The x2, y2 pixels are not included. """ molded_image, window, scale, padding, crop = utils.resize_image( img, min_dim=self.cococonfig.IMAGE_MIN_DIM, min_scale=self.cococonfig.IMAGE_MIN_SCALE, max_dim=self.cococonfig.IMAGE_MAX_DIM, mode=self.cococonfig.IMAGE_RESIZE_MODE) molded_image = model.mold_image(molded_image, self.cococonfig) image_meta = model.compose_image_meta( 0, img.shape, molded_image.shape, window, scale, np.zeros([self.cococonfig.NUM_CLASSES], dtype=np.int32)) anchors = MaskRCNN('inference', self.cococonfig, None).get_anchors(molded_image.shape) return molded_image, image_meta, anchors, window
def main(): array = sys.argv[1:] if os.path.exists(array[0]): path_to_weight = array[0] sys.exit(0) else: print('path to weight does not exist') sys.exit(0) if os.path .exists(array[1]): path_to_image =array[1] else: print('path to image does not exist') sys.exit(0) if float(array[2]) <= 1 and float(array[2]) >= 0: conf=array[2] else: print('confidence must be a float') sys.exit(0) config = TestConfig() config.DETECTION_MIN_CONFIDENCE = conf # define the model rcnn = MaskRCNN(mode='inference', model_dir='./load_weights', config=config) # load coco model weights rcnn.load_weights(path_to_weight, by_name=True) # load photograph img = load_img(path_to_image) img = img_to_array(img) # make prediction results = rcnn.detect([img], verbose=1) # get dictionary for first prediction r = results[0] # show photo with bounding boxes, masks, class labels and scores display_instances(img, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
def predictImage(self, imagePath): testConfig = TestConfig() testConfig.NUM_CLASSES = 1 + len(self.classes) testConfig.IMAGE_META_SIZE = 1 + 3 + 3 + 4 + 1 + testConfig.NUM_CLASSES xmlPath = imagePath[0:imagePath.rfind(".")] + ".xml" rcnn = MaskRCNN(mode='inference', model_dir='./', config=testConfig) # load coco model weights # J. modificar con el path al modelo. rcnn.load_weights(self.modelWeights, by_name=True) # load the input image (in BGR order), clone it, and preprocess it img = load_img(imagePath) img = img_to_array(img) (hI, wI, d) = img.shape # detect objects in the input image and correct for the image scale # Poner short=512 results = rcnn.detect([img], verbose=1) r = results[0] boxes1 = [] for (box, score, cid) in zip(r['rois'], r['scores'], r['class_ids']): if score < self.CONFIDENCE: continue # Añadir label que sera con net.classes[cid] boxes1.append(([self.classes[cid - 1], box], score)) file = open(xmlPath, "w") file.write( self.generateXML( imagePath.split("/")[-1], imagePath[0:imagePath.rfind("/")], wI, hI, d, boxes1)) file.close() self.combineImageAndPrediction(imagePath, xmlPath)
def main(): # load training set (75/15/15 split between train/test/validation) train_set = prep_dataset(os.path.join(DATA_PATH, 'train')) test_set = prep_dataset(os.path.join(DATA_PATH, 'test')) # generate model config = LicensePlateConfig() model = MaskRCNN(mode='training', model_dir=os.path.join(WEIGHT_PATH, 'log/'), config=config) # load pre-trained MS COCO weights model.load_weights(os.path.join(WEIGHT_PATH, 'mask_rcnn_coco.h5'), by_name=True, exclude=[ 'mrcnn_class_logits', 'mrcnn_bbox_fc', 'mrcnn_bbox', 'mrcnn_mask' ]) # train top layer model.train(train_set, test_set, learning_rate=config.LEARNING_RATE, epochs=10, layers='heads') # adjust learning rate for finetuning to avoid overfitting config.LEARNING_RATE = 1e-5 # finetune all layers model.train(train_set, test_set, learning_rate=config.LEARNING_RATE, epochs=5, layers='all')
def __init__(self, mode="inference", model_dir="logs", config=MaskRCNNConfig()): self.model = MaskRCNN(mode=mode, model_dir=model_dir, config=config) self.model.load_weights(DATASET_FILE, by_name=True) self.CLASS_NAMES = [ 'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ] self.detections = None self.person_masks = None self.person_boxes = None
def __init__(self): """ Inicializa o objeto responsavel pela verificação das vagas """ # Root directory of the project ROOT_DIR = Path(".") # Directory to save logs and trained model MODEL_DIR = os.path.join(ROOT_DIR, "logs") # Local path to trained weights file COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") # Download COCO trained weights from Releases if needed if not os.path.exists(COCO_MODEL_PATH): mrcnn.utils.download_trained_weights(COCO_MODEL_PATH) # Directory of images to run detection on IMAGE_DIR = os.path.join(ROOT_DIR, "images") # Video file or camera to process - set this to 0 to use your webcam instead of a video file VIDEO_SOURCE = "test_images/parkingsd.gif" # Create a Mask-RCNN model in inference mode self.model = MaskRCNN( mode="inference", model_dir=MODEL_DIR, config=MaskRCNNConfig()) # Load pre-trained model self.model.load_weights(COCO_MODEL_PATH, by_name=True) # Load the video file we want to run detection on self.video_capture = cv2.VideoCapture(VIDEO_SOURCE) # How many frames of video we've seen in a row with a parking space open self.free_space_frames = 0
def prediction_masks(images, model_option): if model_option=='random': predictions = np.zeros((*images.shape[0:3],5)) for i, image in enumerate(images): predictions[i] = random_predictions(image_size=(350,525)) elif model_option=='fcn': #Local path model_path = 'image_segmentation.h5' #Colab path #model_path = 'drive/My Drive/CS 583/Project/image_segmentation.h5' model = load_model(model_path) predictions = model.predict(images) elif model_option=='mask-rcnn': #Recreate the model in inference mode inference_config = InferenceConfig() model = MaskRCNN(mode='inference', config=inference_config, model_dir='./') #Local path model_path = 'mask_rcnn_clouds_config_0001.h5' #Colab path #model_path = 'drive/My Drive/CS 583/Project/mask_rcnn_clouds_config_0001.h5' #Load trained weights model.load_weights(model_path, by_name=True) samples = np.zeros((images.shape[0],1024,1024,images.shape[-1])) for i, name in enumerate(names): image = Image.open('data/test_images/'+name) image = image.resize((1024,682)) new_im = Image.new("RGB", (1024,1024)) samples[i] = new_im.paste(image, ((0,170))) predictions = model.detect(samples, verbose=0) for i, pred in enumerate(predictions): pred_mask = predictions[i]['masks'] predictions[i] = pred_mask[i][170:854,:] #Trim off top and bottom padding else: print('Error: No such model option.') return predictions
def predict(self, images, verbose=False): ''' ''' if not self.test_model: model = MaskRCNN(mode="inference", config=C.TestingConfig(), model_dir=self.model_dir) weights = model.find_last() model.load_weights(weights, by_name=True) self.test_model = model results = [] for image in images: results.append(self.test_model.detect([image])[0]) if verbose: r = results[0] visualize.display_instances(images[0], r['rois'], r['masks'], r['class_ids'], ["",""], r['scores'],figsize=(10,10)) return results
def define_model(model=None, class_names=None): K.clear_session() if Path(model).exists() == False: model = './djangoserver/mask_rcnn_coco.h5' # define 81 classes that the coco model knowns about class_names = [ 'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ] rcnn = MaskRCNN(mode='inference', config=TestConfig(class_names), model_dir='./djangoserver/') rcnn.load_weights(model, by_name=True) return rcnn, class_names
def get_model(): # Create a Mask-RCNN model in inference mode model = MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=MaskRCNNConfig()) # Load pre-trained model model.load_weights(COCO_MODEL_PATH, by_name=True) return model
def __init__(self): self.CLASS_NAMES = classes self.COLORS = extra.getRandomColors(self.CLASS_NAMES) model = getMaskConfig(float(config.detectionMinConfidence)) self.model = MaskRCNN(mode="inference", model_dir=config.LOGS_DIR, config=model) self.model.load_weights(config.DATASET_DIR, by_name=True)
def creat_instance(): if mrcnn._model == None: MODEL_DIR = os.path.dirname(os.path.abspath(__file__)) + '/object' mrcnn._model = MaskRCNN(mode='inference', model_dir=MODEL_DIR, config=MRCNNConfig()) mrcnn._model.load_weights(MODEL_DIR + '/mask_rcnn_coco.h5', by_name=True)
def load(): global model model = MaskRCNN(mode='inference', config=ConfigInference(), model_dir='/opt/beslim.ai/var/run/model/mrcnn') model.load_weights('/opt/beslim.ai/etc/mask_rcnn_config_train_0022.h5', by_name=True)
def __init__(self): with open(cfg.CLASSES_FILE, 'rt') as file: self.CLASS_NAMES = file.read().rstrip('\n').split('\n') self.COLORS = extra.getRandomColors(self.CLASS_NAMES) self.model = MaskRCNN(mode="inference", model_dir=cfg.LOGS_DIR, config=cfg.MaskRCNNConfig()) self.model.load_weights(cfg.DATASET_DIR, by_name=True)
def initialize_model(self): # create config self.cfg = PredictionConfig() # define the model self.model = MaskRCNN(mode='inference', model_dir='./', config=self.cfg) # load model weights model_path = PRETRAINED_WEIGHT_PATH self.model.load_weights(model_path, by_name=True) self.node.get_logger().info('MODEL IS READY NOW')
def init_model_config(): print("[x] to close") config = PredictionConfig() sourcePath = os.path.dirname(os.path.abspath(__file__)) model = MaskRCNN(mode='inference', model_dir=sourcePath, config=config) modelAbsolutePath = os.path.join(sourcePath, "model.h5") print(modelAbsolutePath) model.load_weights(modelAbsolutePath, by_name=True) return model, config
def load_model(): #Load Prediction config cfg = PredictionConfig() # define the model model = MaskRCNN(mode='inference', model_dir='./', config=cfg) # load model weights model_path = 'model_weights.h5' model.load_weights(model_path, by_name=True) return model, cfg
def __init__(self, networkpath, modelpath): self._config = InferenceConfig() # config.display() self._model = MaskRCNN(mode="inference", model_dir=modelpath, config=self._config) # Load trained weights self._model.load_weights(networkpath, by_name=True)
def train(self, dataset_train, dataset_val, epochs): ''' ''' t0 = time.time() print ('GPU available:', tf.test.is_gpu_available()) self.mode = 'training' self.config = C.TrainingConfig() if not os.path.exists(self.model_dir): os.mkdir(self.model_dir) self.model = MaskRCNN(self.mode, self.config, self.model_dir) # Which weights to start with? # imagenet, coco, or last # Local path to trained weights file COCO_MODEL_PATH = os.path.join(self.model_dir, "mask_rcnn_coco.h5") # Download COCO trained weights from Releases if needed if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) if self.init_with == "imagenet": self.model.load_weights(self.model.get_imagenet_weights(), by_name=True) elif self.init_with == "coco": # Load weights trained on MS COCO, but skip layers that # are different due to the different number of classes # See README for instructions to download the COCO weights self.model.load_weights(COCO_MODEL_PATH, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) elif self.init_with == "last": # Load the last model you trained and continue training self.model.load_weights(model.find_last(), by_name=True) print ('MaskRCNN Setup complete after', time.time()-t0, 'seconds') t0 = time.time() history = History() self.model.train(dataset_train, dataset_val, custom_callbacks=[history], learning_rate=self.config.LEARNING_RATE, epochs=epochs, layers='heads') p.dump(history.history, open(os.path.join(self.model_dir, "history.p"), "wb")) print ('MaskRCNN Training complete after', time.time()-t0, 'seconds') return history
def model_predict_setup(self): """ Setup the model for prediction """ self.model_predict = MaskRCNN(mode='inference', model_dir=self.model_folder, config=self.config) self.model_predict.load_weights(os.path.join(self.model_folder, self.weight_filename), by_name=True)
def before_first_request(): global model, graph, session graph = tf.Graph() session = tf.Session(graph=graph) weight_location = os.path.join('weights', 'weights.h5') with graph.as_default(): with session.as_default(): model = MaskRCNN(mode='inference', config=InferenceConfig(), model_dir=str()) model.load_weights(weight_location, by_name=True)
def predict(): config = PredictionConfig() model_predict = MaskRCNN( mode="inference", model_dir= '../web app implemenation/logs/parking_cfg20200617T0142/mask_rcnn_parking_cfg_0005.h5', config=config) model_predict.load_weights( '../web app implemenation/logs/parking_cfg20200617T0142/mask_rcnn_parking_cfg_0005.h5', by_name=True) return model_predict
def predict(): import numpy as np from mrcnn.model import MaskRCNN from mrcnn.model import mold_image import skimage.io model_config = config.PredictionConfig() model = MaskRCNN(mode='inference', model_dir='./', config=model_config) model.keras_model.metrics_tensors = [] # load model weights model.load_weights(config.keras_model_dir, by_name=True) dir_path = '../data/test' outfile = open('../data/submission.csv', 'w') for id in range(1, 1515): # 读取文件报错,暂不知道解决方案 if id == 50: outfile.write('{},{},{}\n'.format(id, 5, 0)) continue elif id == 227: outfile.write('{},{},{}\n'.format(id, 1, 1)) continue elif id == 1201: outfile.write('{},{},{}\n'.format(id, 2, 0)) continue try: file_path = '{}/{}.jpg'.format(dir_path, id) image = skimage.io.imread(file_path) scaled_image = mold_image(image, model_config) sample = np.expand_dims(scaled_image, 0) yhat = model.detect(sample, verbose=0)[0] except: print(file_path) continue # 按照得分进行排序 indices = np.argsort(yhat["scores"])[::-1] boxes = [] for i in range(len(indices)): boxes.append([ yhat["class_ids"][i] - 1, yhat['rois'][i][1], yhat['rois'][i][0], yhat['rois'][i][3], yhat['rois'][i][2] ]) boxes = np.array(boxes) boxes = boxes[indices] hat = 0 person = 0 for box in boxes: label = box[0] if label == 0: hat += 1 else: person += 1 outfile.write('{},{},{}\n'.format(id, hat, person)) outfile.close()
def run(train_csv, imagedir, model_path): train_data = utils.load(train_csv) classes = utils.determine_classes(train_data) train_set = prepare_dataset(train_data, imagedir, classes) cfg = PredictionConfig() model = MaskRCNN(mode="inference", model_dir=imagedir, config=cfg) model.load_weights(model_path, by_name=True) train_mAP = evaluate_model(train_set, model, cfg, classes, model_path) print("Train mAP: %.3f" % train_mAP)
def run(csv, model_dir, model_file, img_name): data = utils.load(csv) classes = utils.determine_classes(data) train_set = prepare_dataset(data, model_dir, classes) cfg = PredictionConfig() model = MaskRCNN(mode="inference", model_dir=model_dir, config=cfg) model.load_weights(model_file, by_name=True) image_path = f"{model_dir}/{img_name}" plot(train_set, model, cfg, image_path, classes) pyplot.show()
def predictImage(train_set, test_set, modelPath): # create config cfg = PredictionConfig() # define the model model = MaskRCNN(mode='inference', model_dir='./model', config=cfg) # load model weights model_path = modelPath model.load_weights(model_path, by_name=True) # plot predictions for train dataset plot_actual_vs_predicted(train_set, model, cfg) # plot predictions for test dataset plot_actual_vs_predicted(test_set, model, cfg)
def run(train_csv, test_csv, imagedir, model_path): data = utils.load(train_csv) classes = utils.determine_classes(data) test_data = utils.load(test_csv, is_train=False) test_set = prepare_dataset(test_data, imagedir, classes) cfg = PredictionConfig() model = MaskRCNN(mode="inference", model_dir=imagedir, config=cfg) model.load_weights(model_path, by_name=True) evaluate_model(test_set, model, cfg, classes, model_path) print("Done!")