def get_mark_dicts(data_type): data = pd.read_csv(data_dir + data_type + "/annotations/1person_IDs.csv") person_ids = list(data.iloc[:, 1].values) # Load dataset dataset = coco.CocoDataset() dataset.load_coco(data_dir+data_type, subset= data_type, year="2017", class_ids = [1], image_ids=person_ids) # Must call before using the dataset dataset.prepare() imgs_anns = dataset.image_info dataset_dicts = [] for idx, v in enumerate(imgs_anns): record = {} record["file_name"] = v["path"] record["image_id"] = idx record["height"] = v["height"] record["width"] = v["width"] annos = v["annotations"] objs = [] for anno in annos: obj = { "area": anno["area"], "iscrowd": anno["iscrowd"], "bbox": anno["bbox"], "bbox_mode": BoxMode.XYWH_ABS, # "segmentation": anno["segmentation"], "category_id": anno["category_id"], } objs.append(obj) record["annotations"] = objs dataset_dicts.append(record) return dataset_dicts
def main(self): print("detecting cars...") """Create a model object and load the weights.""" model = mrcnn.model.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=self.config) model.load_weights(COCO_MODEL_PATH, by_name=True) """Check class numbers""" dataset = coco.CocoDataset() dataset.load_coco(MODEL_DIR, "train") dataset.prepare() """Load an image""" IMAGE = self.dir #os.path.abspath("../../resources/images/stjohns.jpg") image = skimage.io.imread(IMAGE) results = model.detect([image], verbose=1) """Visualize results""" r = results[0] r = self.filter_vehicles(r) """Save image""" t = int(time.time()) name = str(t) + ".png" path = os.path.abspath("../output") mrcnn.visualize.display_instances(path, name, image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'], title='# os detect cars: {}'.format( len(r['class_ids']))) return len(r['class_ids'])
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU GPU_COUNT = 1 IMAGES_PER_GPU = 1 config = InferenceConfig() config.display() COCO_DIR = "../coco/images" # TODO: enter value here if not os.path.exists('./image_all'): os.mkdir('./image_all') # Load dataset dataset = coco.CocoDataset() dataset.load_coco(COCO_DIR, "train") # Must call before using the dataset dataset.prepare() print("Image Count: {}".format(len(dataset.image_ids))) print("Class Count: {}".format(dataset.num_classes)) for i, info in enumerate(dataset.class_info): print("{:3}. {:50}".format(i, info['name'])) #Load model model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) n_num = 80000
import sys import imgaug ## Import Mobile Mask R-CNN from mmrcnn import model as modellib, utils import coco ## Paths ROOT_DIR = os.getcwd() MODEL_DIR = os.path.join(ROOT_DIR, "logs") COCO_DIR = os.path.join(ROOT_DIR, 'data/coco') DEFAULT_MODEL_DIR = os.path.join(MODEL_DIR, "mask_rcnn_256_cocoperson_0283.h5") ## Dataset class_names = ['person'] # all classes: None dataset_train = coco.CocoDataset() dataset_train.load_coco(COCO_DIR, "train", class_names=class_names) dataset_train.prepare() dataset_val = coco.CocoDataset() dataset_val.load_coco(COCO_DIR, "val", class_names=class_names) dataset_val.prepare() ## Model config = coco.CocoConfig() config.display() model = modellib.MaskRCNN(mode="training", model_dir=MODEL_DIR, config=config) model.keras_model.summary() ## Weights model_path = model.get_imagenet_weights() #model_path = model.find_last()[1]
# Get path to saved weights # Either set a specific path or find last trained weights # model_path = os.path.join(ROOT_DIR, ".h5 file name here") # model_path = model.find_last()[1] model_path = os.path.join(ROOT_DIR, weight_file) # Load trained weights (fill in path to trained weights here) assert model_path != "", "Provide path to trained weights" print("Loading weights from ", model_path) model.load_weights(model_path, by_name=True) # Load dataset assert inference_config.NAME == "coco" val_dataset_keypoints = coco.CocoDataset(task_type="person_keypoints") val_dataset_keypoints.load_coco(COCO_DIR, "val", 2017) val_dataset_keypoints.prepare() print("Val Keypoints Image Count: {}".format(len(val_dataset_keypoints.image_ids))) print("Val Keypoints Class Count: {}".format(val_dataset_keypoints.num_classes)) for i, info in enumerate(val_dataset_keypoints.class_info): print("{:3}. {:50}".format(i, info['name'])) AP = [] OKS = [] std = np.array([[0.026], [0.025], [0.025], [0.035], [0.035], [0.079], [0.079], [0.072], [0.072], [0.062], [0.062], [0.107], [0.107], [0.087], [0.087], [0.089], [0.089]])
TIMESTEP = 5 #%% Création du détecteur smartmov = SmartMov() #%% Chargement du U-Net smartmov.load_models('unet', model_unet=MODELS_UNET_DIR + "unet_highway.h5", shape_unet=s, timestep=TIMESTEP) #%% Load dataset train COCO_DIR = os.path.join(DATASET_DIR, "coco2014/") coco_train = coco.CocoDataset( ) # Objet qui va contenir les images et les masques d'entrainement coco_train.load_coco(COCO_DIR, "train", year='2014', auto_download=True, class_ids=[1, 3]) coco_train.prepare() coco_val = coco.CocoDataset( ) # Objet qui va contenir les images et les masques de validation cc = coco_val.load_coco(COCO_DIR, "minival", year='2014', auto_download=True, return_coco=True, class_ids=[1, 3])
config = coco.CocoConfig() model = modellib.MaskRCNN(mode="inference", config=config, model_dir=MODEL_DIR) # Get path to saved weights # Either set a specific path or find last trained weights #model_path = os.path.join(MODEL_DIR, "mask_rcnn_512_cocoperson_0396.h5") model_path = model.find_last()[1] # Load trained weights (fill in path to trained weights here) assert model_path != "", "Provide path to trained weights" print("> Loading weights from {}".format(model_path)) model.load_weights(model_path, by_name=True) # Dataset class_names = ['person'] # all classes: None dataset_val = coco.CocoDataset() COCO = dataset_val.load_coco(COCO_DIR, "val", class_names=class_names, return_coco=True) dataset_val.prepare() print("> Running COCO evaluation on {} images.".format(NUM_EVALS)) coco.evaluate_coco(model, dataset_val, COCO, "bbox", limit=NUM_EVALS) model.keras_model.save(MODEL_DIR + "/mobile_mask_rcnn_{}.h5".format(config.NAME)) # Test on a random image image_id = random.choice(dataset_val.image_ids) original_image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset_val, config, image_id, use_mini_mask=False)
def generate(): try: from . import coco from .coco_classes import coco_classes, calc_classes, calc_class_names except: import coco from coco_classes import coco_classes, calc_classes, calc_class_names import cv2 if not os.path.isdir(FLAGS.output_dir): os.mkdir(FLAGS.output_dir) train_writers = [] for ii in range(FLAGS.num_files): train_writers.append(None if FLAGS.debug else \ tf.python_io.TFRecordWriter(FLAGS.output_dir + "train_data%d.tfrecord" % ii)) val_writer = None if FLAGS.debug else \ tf.python_io.TFRecordWriter(FLAGS.output_dir + "validation_data.tfrecord") nclasses = len(calc_classes.keys()) class_percents = np.zeros((nclasses), dtype=np.float32) for split, writer in [('train', train_writers), ('val', val_writer)]: # Load dataset dataset = coco.CocoDataset() dataset.load_coco(FLAGS.coco_root, split) # Must call before using the dataset dataset.prepare() print("Image Count: {}".format(len(dataset.image_ids))) print("COCO Class Count: {}".format(dataset.num_classes)) print("CALC Class Count: {}".format(nclasses)) # for i, info in enumerate(dataset.class_info): # print("{:3}. {:50}".format(i, info['name'])) count = 1 for image_id in tqdm(dataset.image_ids): # print("Working on sample %d" % image_id) if split == 'val': # height, width, channels = img.shape # print('image height, width, channel:', height, width, channels) cl_live = cv2.cvtColor(cv2.resize( cv2.imread("CampusLoopDataset/live/Image%s.jpg" % (str(count).zfill(3))), (vw, vh), interpolation=cv2.INTER_CUBIC), cv2.COLOR_BGR2RGB) cl_mem = cv2.cvtColor(cv2.resize( cv2.imread("CampusLoopDataset/memory/Image%s.jpg" % (str(count).zfill(3))), (vw, vh), interpolation=cv2.INTER_CUBIC), cv2.COLOR_BGR2RGB) image = cv2.resize(dataset.load_image(image_id), (vw, vh), interpolation=cv2.INTER_CUBIC) masks, class_ids = dataset.load_mask(image_id) mask_label = np.zeros((vh, vw, nclasses), dtype=np.bool) for i in range(masks.shape[2]): cid = calc_classes[coco_classes[class_ids[i]][1]] mask_label[:, :, cid] = np.logical_or(mask_label[:, :, cid], cv2.resize(masks[:, :, i].astype(np.uint8), (vw, vh), interpolation=cv2.INTER_NEAREST).astype(np.bool)) # No labels for BG. Make them! mask_label[:, :, 0] = np.logical_not(np.any(mask_label[:, :, 1:], axis=2)) if split == 'train': cp = np.mean(mask_label, axis=(0, 1)) class_percents += (1.0 / count) * (cp - class_percents) mask = np.argmax(mask_label, axis=-1) if FLAGS.debug: rgb = np.zeros((vh, vw, 3)) legend = [] np.random.seed(0) for i in range(nclasses): c = np.random.rand(3) case = mask == i if np.any(case): legend.append(Patch(facecolor=tuple(c), edgecolor=tuple(c), label=calc_class_names[i])) rgb[case, :] = c _image = cv2.resize(image, (vw, vh)) / 255.0 _image = 0.3 * _image + 0.7 * rgb global imdata if imdata is None: imdata = plt.imshow(_image) f = plt.gca() f.axes.get_xaxis().set_ticks([]) f.axes.get_yaxis().set_ticks([]) else: imdata.set_data(_image) lgd = plt.legend(handles=legend, loc='upper left', bbox_to_anchor=(1.0, 1)) plt.pause(1e-9) plt.draw() plt.pause(3) else: features_ = { 'img': bytes_feature(tf.compat.as_bytes(image.tostring())), 'label': bytes_feature(tf.compat.as_bytes(mask.astype(np.uint8).tostring())) } if split == 'val': features_['cl_live'] = bytes_feature(tf.compat.as_bytes(cl_live.tostring())), features_['cl_mem'] = bytes_feature(tf.compat.as_bytes(cl_mem.tostring())), example = tf.train.Example(features=tf.train.Features(feature=features_)) if split == 'val': writer.write(example.SerializeToString()) else: writer[np.random.randint(0, FLAGS.num_files)].write(example.SerializeToString()) if split == 'val' and image_id == 99: break count += 1
config = InferenceConfig() # config.display() # Get the first available GPU DEVICE_ID_LIST = GPUtil.getFirstAvailable() DEVICE_ID = DEVICE_ID_LIST[0] # grab first element from list # Set CUDA_VISIBLE_DEVICES to mask out all other GPUs than the first available device id os.environ["CUDA_VISIBLE_DEVICES"] = str(DEVICE_ID) # Training dataset. Use the training set and 35K from the # validation set, as as in the Mask RCNN paper. year = 2014 dataset_train = coco.CocoDataset() dataset_train.load_coco(COCO_DIR, "train", year=year, auto_download=True) dataset_train.load_coco(COCO_DIR, "valminusminival", year=year, auto_download=True) dataset_train.prepare() def mAP_test(image_names, image_dir, file_dir): APs = [] t1 = time.time() for image_name in image_names: image = skimage.io.imread(os.path.join(image_dir, image_name)) image, window, scale, padding = utils.resize_image( image,
def get(self, picture_name): s3 = boto3.resource('s3') obj = s3.Object(bucket_name='ndpainteddogs', key=picture_name) response = obj.get() data = response['Body'].read() nparr = np.fromstring(data, np.uint8) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) imor = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Root directory of the project ROOT_DIR = os.path.abspath(".") # Import Mask RCNN sys.path.append(ROOT_DIR) # To find local version of the library from mrcnn import utils from mrcnn import visualize from mrcnn.visualize import display_images import mrcnn.model as modellib from mrcnn.model import log # 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): utils.download_trained_weights(COCO_MODEL_PATH) # MS COCO Dataset import coco config = coco.CocoConfig() COCO_DIR = "./coco" # TODO: enter value here # Override the training configurations with a few # changes for inferencing. class InferenceConfig(config.__class__): # Run detection on one image at a time GPU_COUNT = 1 IMAGES_PER_GPU = 1 config = InferenceConfig() config.display() # Device to load the neural network on. # Useful if you're training a model on the same # machine, in which case use CPU and leave the # GPU for training. DEVICE = "/cpu:0" # /cpu:0 or /gpu:0 # Inspect the model in training or inference modes # values: 'inference' or 'training' # TODO: code for 'training' test mode not ready yet TEST_MODE = "inference" # Build validation dataset dataset = coco.CocoDataset() dataset.load_coco(COCO_DIR, "minival") # Must call before using the dataset dataset.prepare() # Create model in inference mode with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) # Set weights file path weights_path = COCO_MODEL_PATH # Load weights print("Loading weights ", weights_path) model.load_weights(weights_path, by_name=True) from skimage.transform import resize im = resize(imor, (1024, 1024), mode="constant", preserve_range=True) # Run object detection results = model.detect([im], verbose=1) def apply_mask(image, mask, color, alpha=1): """Apply the given mask to the image. """ for c in range(3): image[:, :, c] = np.where( mask == 0, image[:, :, c] * (1 - alpha) + alpha * color[c] * 255, image[:, :, c]) return image # Display results r = results[0] unique_class_ids = np.unique(r['class_ids']) mask_area = [ np.sum(r['masks'][:, :, np.where(r['class_ids'] == i)[0]]) for i in unique_class_ids ] top_ids = [ v[0] for v in sorted(zip(unique_class_ids, mask_area), key=lambda r: r[1], reverse=True) if v[1] > 0 ] # Generate images and titles for i in range(1): class_id = top_ids[i] if i < len(top_ids) else -1 # Pull masks of instances belonging to the same class. m = r['masks'][:, :, np.where(r['class_ids'] == class_id)[0]] m = np.sum(m * np.arange(1, m.shape[-1] + 1), -1) image_cropped = im.astype(np.uint32).copy() image_cropped = apply_mask(image_cropped, m, (0, 0, 0)) TARGET_PIXEL_AREA = 160000.0 ratio = float(imor.shape[1]) / float(imor.shape[0]) """Calculate the new height""" new_h = int(math.sqrt(TARGET_PIXEL_AREA / ratio) + 0.5) """Calculate the new width""" new_w = int((new_h * ratio) + 0.5) imnew = image_cropped.astype(np.uint8) imnew = cv2.resize(imnew, (new_w, new_h)) almost = cv2.imencode('.jpg', cv2.cvtColor(imnew, cv2.COLOR_RGB2BGR))[1].tostring() #return flask.send_file(io.BytesIO(almost), mimetype='image/jpeg') s3.Bucket('ndprocessedimages').put_object(Key=picture_name, Body=almost) response = {"statusCode": 200} return response