def _generate_background(self, image, bboxes): _, H, W = image.shape bbox = generate_background_bbox((H, W), (224, 224), bboxes) ymin, xmin, ymax, xmax = bbox image = resnet.prepare(image[:, ymin:ymax, xmin:xmax]) label = len(roaddamage_label_names) + 1 return image, label
def feature_extract(img, model): img = resnet.prepare(img, size=None) img = np.asarray([img], dtype=np.float32) img = Variable(cuda.to_gpu(img)) with chainer.using_config("train", False): feature = cuda.to_cpu(model(img).data) return feature
def preprocess(image, model_name): if model_name == "mv2": image /= 128. image = transforms.resize(image, (224, 224)) elif model_name == "vgg16": image = vgg.prepare(image, size=(224, 224)) elif model_name == "resnet50": image = resnet.prepare(image, size=(224, 224)) else: raise Exception("illegal model") return image
def _generate_damage(self, image, bboxes, labels): index = np.random.randint(len(labels)) label = labels[index] ymin, xmin, ymax, xmax = bboxes[index] damage = image[:, ymin:ymax, xmin:xmax] background = np.zeros(image.shape) background[:, ymin:ymax, xmin:xmax] = damage image = resnet.prepare(background) return image, label
def _layer_out(self, images, layer): layers = ['conv1', 'bn1', 'res2', 'res3', 'res4', 'res5'] if layer not in layers: raise ValueError('Layer {} does not exist.'.format(layer)) x = concat_examples([prepare(img) for img in images]) with chainer.function.no_backprop_mode(), chainer.using_config( 'train', False): x = Variable(self.xp.asarray(x)) y = self(x, layers=[layer])[layer] return y
def test_prepare(self): x1 = numpy.random.uniform(0, 255, (320, 240, 3)).astype(numpy.uint8) x2 = numpy.random.uniform(0, 255, (320, 240)).astype(numpy.uint8) x3 = numpy.random.uniform(0, 255, (160, 120, 3)).astype(self.dtype) x4 = numpy.random.uniform(0, 255, (1, 160, 120)).astype(self.dtype) x5 = numpy.random.uniform(0, 255, (3, 160, 120)).astype(numpy.uint8) y1 = resnet.prepare(x1) assert y1.shape == (3, 224, 224) assert y1.dtype == self.dtype y2 = resnet.prepare(x2) assert y2.shape == (3, 224, 224) assert y2.dtype == self.dtype y3 = resnet.prepare(x3, size=None) assert y3.shape == (3, 160, 120) assert y3.dtype == self.dtype y4 = resnet.prepare(x4) assert y4.shape == (3, 224, 224) assert y4.dtype == self.dtype y5 = resnet.prepare(x5, size=None) assert y5.shape == (3, 160, 120) assert y5.dtype == self.dtype
def test_prepare(self): x1 = numpy.random.uniform(0, 255, (320, 240, 3)).astype(numpy.uint8) x2 = numpy.random.uniform(0, 255, (320, 240)).astype(numpy.uint8) x3 = numpy.random.uniform(0, 255, (160, 120, 3)).astype(numpy.float32) x4 = numpy.random.uniform(0, 255, (1, 160, 120)).astype(numpy.float32) x5 = numpy.random.uniform(0, 255, (3, 160, 120)).astype(numpy.uint8) y1 = resnet.prepare(x1) self.assertEqual(y1.shape, (3, 224, 224)) self.assertEqual(y1.dtype, numpy.float32) y2 = resnet.prepare(x2) self.assertEqual(y2.shape, (3, 224, 224)) self.assertEqual(y2.dtype, numpy.float32) y3 = resnet.prepare(x3, size=None) self.assertEqual(y3.shape, (3, 160, 120)) self.assertEqual(y3.dtype, numpy.float32) y4 = resnet.prepare(x4) self.assertEqual(y4.shape, (3, 224, 224)) self.assertEqual(y4.dtype, numpy.float32) y5 = resnet.prepare(x5, size=None) self.assertEqual(y5.shape, (3, 160, 120)) self.assertEqual(y5.dtype, numpy.float32)
def test_prepare(self): x1 = numpy.random.uniform(0, 255, (320, 240, 3)).astype(numpy.uint8) x2 = numpy.random.uniform(0, 255, (320, 240)).astype(numpy.uint8) x3 = numpy.random.uniform(0, 255, (160, 120, 3)).astype(self.dtype) x4 = numpy.random.uniform(0, 255, (1, 160, 120)).astype(self.dtype) x5 = numpy.random.uniform(0, 255, (3, 160, 120)).astype(numpy.uint8) y1 = resnet.prepare(x1) assert y1.shape == (3, 224, 224) assert y1.dtype == self.dtype y2 = resnet.prepare(x2) assert y2.shape == (3, 224, 224) assert y2.dtype == self.dtype y3 = resnet.prepare(x3, size=None) assert y3.shape == (3, 160, 120) assert y3.dtype == self.dtype y4 = resnet.prepare(x4) assert y4.shape == (3, 224, 224) assert y4.dtype == self.dtype y5 = resnet.prepare(x5, size=None) assert y5.shape == (3, 160, 120) assert y5.dtype == self.dtype
def prepare_images(self, images): if self.xp != np: device = images.data.device images = F.copy(images, -1) converted_images = [ resnet.prepare(image.data, size=None) for image in F.separate(images, axis=0) ] converted_images = F.stack(converted_images, axis=0) if self.xp != np: converted_images = F.copy(converted_images, device.id) return converted_images
def img2resnet(img, xp=np, dtype=np.float32): dst = resnet.prepare(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) ch, w, h = dst.shape return xp.array(dst, dtype=dtype).reshape(-1, ch, w, h)
def imgs2resnet(imgs, xp=np): dst = [ resnet.prepare(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) for img in imgs ] return xp.array(dst)
def extract_feature(image, model): x = resnet.prepare(image, size=None) x = chainer.Variable(xp.asarray([x], dtype=xp.float32)) with chainer.using_config('train', False): feature = model(x) return feature
def get_example(self, i): image, label = self.base[i] if self.crop: image = self.random_crop(image) image = prepare(image) return image, label
def __call__(self, in_data): img = in_data[0] img = resnet.prepare(img, (self.size, self.size)) return (img, *in_data[1:])