Exemple #1
0
def classify(request):
    if request.method == 'POST' and request.FILES['predict_img']:
        predict_img = request.FILES['predict_img']
        fs = FileSystemStorage()
        filename = fs.save(predict_img.name, predict_img)
        uploaded_file_url = fs.url(filename)

        # 推論処理
        img = cv2.cvtColor(cv2.imread('media/{}'.format(filename)),
                           cv2.COLOR_BGR2RGB)
        x = prepare(img)
        y = model.predictor(np.array([x]))
        y_proba = F.softmax(y).data
        y_pre = np.argmax(y_proba, axis=1)[0]
        proba = round(y_proba[0][y_pre] * 100, 2)

        # AnimalInfoのDBから必要な情報の取得
        animal_info = AnimalInfo.objects.filter(animal_id=y_pre)

        # ZooCollectionに情報を保存
        current_user = request.user
        if not list(
                ZooCollection.objects.filter(user_id=current_user.id,
                                             animal_id=y_pre)):
            user_info = ZooCollection(user_id=current_user.id, animal_id=y_pre)
            user_info.save()

        return render(
            request, 'zoo_app/classify.html', {
                'uploaded_file_url': uploaded_file_url,
                'animal_info': animal_info,
                'proba': proba
            })

    return render(request, 'zoo_app/classify.html', {})
Exemple #2
0
    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 = googlenet.prepare(x1)
        assert y1.shape == (3, 224, 224)
        assert y1.dtype, self.dtype
        y2 = googlenet.prepare(x2)
        assert y2.shape == (3, 224, 224)
        assert y2.dtype, self.dtype
        y3 = googlenet.prepare(x3, size=None)
        assert y3.shape == (3, 160, 120)
        assert y3.dtype, self.dtype
        y4 = googlenet.prepare(x4)
        assert y4.shape == (3, 224, 224)
        assert y4.dtype, self.dtype
        y5 = googlenet.prepare(x5, size=None)
        assert y5.shape == (3, 160, 120)
        assert y5.dtype, self.dtype
Exemple #3
0
    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 = googlenet.prepare(x1)
        self.assertEqual(y1.shape, (3, 224, 224))
        self.assertEqual(y1.dtype, numpy.float32)
        y2 = googlenet.prepare(x2)
        self.assertEqual(y2.shape, (3, 224, 224))
        self.assertEqual(y2.dtype, numpy.float32)
        y3 = googlenet.prepare(x3, size=None)
        self.assertEqual(y3.shape, (3, 160, 120))
        self.assertEqual(y3.dtype, numpy.float32)
        y4 = googlenet.prepare(x4)
        self.assertEqual(y4.shape, (3, 224, 224))
        self.assertEqual(y4.dtype, numpy.float32)
        y5 = googlenet.prepare(x5, size=None)
        self.assertEqual(y5.shape, (3, 160, 120))
        self.assertEqual(y5.dtype, numpy.float32)
Exemple #4
0
    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 = googlenet.prepare(x1)
        assert y1.shape == (3, 224, 224)
        assert y1.dtype, self.dtype
        y2 = googlenet.prepare(x2)
        assert y2.shape == (3, 224, 224)
        assert y2.dtype, self.dtype
        y3 = googlenet.prepare(x3, size=None)
        assert y3.shape == (3, 160, 120)
        assert y3.dtype, self.dtype
        y4 = googlenet.prepare(x4)
        assert y4.shape == (3, 224, 224)
        assert y4.dtype, self.dtype
        y5 = googlenet.prepare(x5, size=None)
        assert y5.shape == (3, 160, 120)
        assert y5.dtype, self.dtype
Exemple #5
0
def create_datasets(groups=500, imgpath='', savepath=''):
#def create_datasets(groups=500, imgpath='D:\BaiduNetdiskDownload\ILSVRC2012_img_val\\', savepath='E:\ccc\imgroup'):
    if not imgpath:
        raise ValueError
    if not savepath:
        raise ValueError
    img_lists = [[] for _ in range(groups)]
    tmp_lists = [[] for _ in range(groups)]
    for index, filename in enumerate(glob.glob(imgpath + '*.JPEG')):
                img_lists[index//(int(50000/groups))].append(filename)
    # print(numpy.array(img_lists).shape) -> (500,100)
    for index, namelists in enumerate(img_lists[0:10]):
        for jpgnames in namelists:
            im = Image.open(jpgnames)
            tmp_lists[index].append(prepare(im, size=(256,256)).tolist())
            im.close()
        numpy.save(savepath + str(index), numpy.array(tmp_lists[index], dtype=numpy.float32))
        tmp_lists[index] = []