@ -1,164 +0,0 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
CIFAR10
|
||||
model
|
||||
logs
|
||||
.idea
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
@ -1,44 +0,0 @@
|
||||
|
||||
from torch import nn
|
||||
|
||||
|
||||
cfg = {'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M']}
|
||||
class QCQ(nn.Module):
|
||||
|
||||
def __init__(self, vgg):
|
||||
super(QCQ, self).__init__()
|
||||
self.features = self._make_layers(vgg)
|
||||
self.dense = nn.Sequential(
|
||||
nn.Linear(512, 4096),
|
||||
nn.ReLU(inplace=True),
|
||||
nn.Dropout(0.4),
|
||||
nn.Linear(4096, 4096),
|
||||
nn.ReLU(inplace=True),
|
||||
nn.Dropout(0.4),
|
||||
)
|
||||
self.classifier = nn.Linear(4096, 10)
|
||||
|
||||
def forward(self, x):
|
||||
out = self.features(x)
|
||||
out = out.view(out.size(0), -1)
|
||||
out = self.dense(out)
|
||||
out = self.classifier(out)
|
||||
return out
|
||||
|
||||
def _make_layers(self, vgg):
|
||||
layers = []
|
||||
in_channels = 3
|
||||
for x in vgg:
|
||||
if x == 'M':
|
||||
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
|
||||
else:
|
||||
layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
|
||||
nn.BatchNorm2d(x),
|
||||
nn.ReLU(inplace=True)]
|
||||
in_channels = x
|
||||
|
||||
layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
|
||||
return nn.Sequential(*layers)
|
||||
|
||||
|
||||
|
||||
@ -1,10 +1,2 @@
|
||||
# CIFAR10
|
||||
CIFAR10学习
|
||||
|
||||
我的环境:
|
||||
python 3.6.13
|
||||
torch 1.10.2
|
||||
cuda 11.3
|
||||
torchvision 0.11.3
|
||||
pillow 8.4.0
|
||||
tensorboard 2.10.1
|
||||
@ -1,97 +0,0 @@
|
||||
import os
|
||||
|
||||
import PIL
|
||||
import torchvision.transforms
|
||||
from PIL import Image
|
||||
|
||||
|
||||
from QCQ_VGG16 import *
|
||||
from QCQ_ResNet18 import *
|
||||
|
||||
CIFAR10_class = ['airplane','automobile','brid','cat','deer','dog','frog','horse','ship','truck']
|
||||
vgg = [96, 96, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M']
|
||||
|
||||
def readImage(img_path='img/test.png'):
|
||||
img = Image.open(img_path).convert('RGB')
|
||||
transform= torchvision.transforms.Compose([torchvision.transforms.Resize((32,32)),torchvision.transforms.ToTensor()])
|
||||
img = transform(img)
|
||||
print(img.shape)
|
||||
img = torch.reshape(img, (1, 3, 32, 32))
|
||||
return img
|
||||
|
||||
|
||||
def DrawImageTxt(imageFile, targetImageFile, txtnum):
|
||||
# 设置字体大小
|
||||
font = PIL.ImageFont.truetype('img/abc.ttf', 50)
|
||||
# 打开文件
|
||||
im = Image.open(imageFile)
|
||||
# 字体坐标
|
||||
draw = PIL.ImageDraw.Draw(im)
|
||||
draw.text((0, 0), txtnum, (255, 255, 0), font=font)
|
||||
# 保存
|
||||
im.save(targetImageFile)
|
||||
# 关闭
|
||||
im.close()
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
print("通过训练好的模型识别十种事物:飞机,汽车,鸟,猫,鹿,狗,青蛙,马,船,卡车")
|
||||
device = 'cuda'
|
||||
models=[]
|
||||
i=1
|
||||
for root, dirs, files in os.walk('model'):
|
||||
for file in files:
|
||||
if file.__contains__('.pth'):
|
||||
file_path=root+'/'+file
|
||||
models.append(file_path)
|
||||
print(f'{i}.'+file)
|
||||
i+=1
|
||||
if models.__len__()==0:
|
||||
print('model文件夹中没有pth模型文件')
|
||||
else:
|
||||
select = int(input('选择一个模型\n'))
|
||||
model_path=models[select-1]
|
||||
if model_path.__contains__('VGG16'):
|
||||
qcq_test=QCQ(vgg)
|
||||
elif model_path.__contains__('RestNet18'):
|
||||
qcq_test=ResNet18()
|
||||
else:
|
||||
print('选择的模型名称中既不包含"VGG16",也不包含"RestNet18"')
|
||||
|
||||
qcq_test.load_state_dict(torch.load(model_path, map_location=torch.device(device)))
|
||||
|
||||
imgs=[]
|
||||
i=1
|
||||
for root, dirs, files in os.walk('img'):
|
||||
for file in files:
|
||||
if file.__contains__('.png') or file.__contains__('.jpg'):
|
||||
if not file.__contains__('pre'):
|
||||
file_path = root + '/' + file
|
||||
imgs.append(file_path)
|
||||
print(f'{i}.' + file)
|
||||
i+=1
|
||||
if imgs.__len__() == 0:
|
||||
print('img文件夹中没有图片')
|
||||
else:
|
||||
select = int(input('选择一个测试图片,\n'))
|
||||
image_path = imgs[select-1]
|
||||
image_name,image_type = image_path.split('.')
|
||||
targetImageFile=image_name+'_pre.png'
|
||||
img=readImage(image_path)
|
||||
qcq_test.eval()
|
||||
with torch.no_grad():
|
||||
output = qcq_test(img)
|
||||
pre=output.argmax(1)
|
||||
txtnum = CIFAR10_class[pre.item()]
|
||||
DrawImageTxt(image_path, targetImageFile, txtnum)
|
||||
print(output)
|
||||
print(pre)
|
||||
print(txtnum)
|
||||
Image.open(targetImageFile).show()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 555 KiB |
|
Before Width: | Height: | Size: 273 KiB |
|
Before Width: | Height: | Size: 185 KiB |
|
Before Width: | Height: | Size: 299 KiB |
Loading…
Reference in new issue