OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 5092|回复: 37

SD卡实验代码

[复制链接]

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
发表于 2019-3-5 00:10:50 | 显示全部楼层 |阅读模式
SD卡实验代码分享

SD.rar

1.85 MB, 下载次数: 445

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-5-8 22:43:15 | 显示全部楼层
ucosiii 第一部分ppt讲解

1.rar

14.89 MB, 下载次数: 70

ucosiii 第一部分ppt讲解

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-5-8 22:44:54 | 显示全部楼层
ucosiii 第二部分ppt讲解

2.rar

14.96 MB, 下载次数: 65

ucosiii 第二部分ppt讲解

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-5-8 22:46:17 | 显示全部楼层
ucosiii 第三部分ppt讲解

3.rar

12 MB, 下载次数: 48

ucosiii 第三部分ppt讲解

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-5-16 23:15:35 | 显示全部楼层
音频讲解PPt

音频.rar

14.07 MB, 下载次数: 53

音频讲解PPt

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-7-5 01:14:48 | 显示全部楼层
好好好好这个是jz2440 烧写流程

百问网精智JZ2440开发板4.3寸LCD版使用手册.pdf

3.32 MB, 下载次数: 25

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-7-5 12:31:40 | 显示全部楼层
111111111111111111111

STM32使用手册.pdf

3.32 MB, 下载次数: 28

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-7-22 01:21:22 | 显示全部楼层
11111111111
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-7-22 01:21:54 | 显示全部楼层
啊啊啊啊啊啊啊啊
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-8-20 07:17:05 | 显示全部楼层
这是一个文档,分享

eee.rar

11.86 KB, 下载次数: 10

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-8-20 07:17:44 | 显示全部楼层
这是一个文档分享分享1111111

eee.rar

11.86 KB, 下载次数: 4

回复 支持 反对

使用道具 举报

1

主题

6

帖子

0

精华

新手上路

积分
28
金钱
28
注册时间
2019-8-2
在线时间
7 小时
发表于 2019-8-20 15:09:09 | 显示全部楼层
楼主好人
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-8-28 12:45:17 | 显示全部楼层
这是个patch包,分享给大家

111.rar

356.6 KB, 下载次数: 6

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-10-4 22:41:10 | 显示全部楼层
11111111111111111111111

Python.rar

5.92 MB, 下载次数: 10

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-10-4 22:46:04 | 显示全部楼层
这是一个python教程,分享给大家看看。。。。。。

Python.rar

5.92 MB, 下载次数: 5

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-10-23 00:17:45 | 显示全部楼层
import pygame,sys,time,random


SCEEEN_X = 600
SCREEN_Y = 560


def main():
    allpos = [1]
    xallpos = [1]
    pygame.init()
    screen_size = (SCEEEN_X, SCREEN_Y)
    screen = pygame.display.set_mode(screen_size)       #创建窗口
    pygame.display.set_caption('‘飞机历险记’')   #设置标题
    clock = pygame.time.Clock()                         #获取时钟
    #加载背景图片
    bg = pygame.image.load("./fly/back2.jpg")
    bgpositon = bg.get_rect()

    hero = pygame.image.load("./fly/fly1.png")      #加载飞机图片
    heropostion = hero.get_rect()                      #获取图片的矩形大小   这应该是 pygame的特点, img这一点load 了图片就已经存在一些属性
    herospeed = [2, 0]

    bullet = pygame.image.load("./fly/bullet.png")      #加载飞机图片
    bulletpostion = bullet.get_rect()                      #获取图片的矩形大小   这应该是 pygame的特点, img这一点load 了图片就已经存在一些属性
    bulletspeed = [2, 0]


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # 如果游戏事件发生,并且等于退出事件,那么系统退出
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:          #如果按键向左
                    herospeed = [-20,0]
                if event.key ==  pygame.K_RIGHT:       #如果按键向右
                    herospeed = [20,0]
                if event.key == pygame.K_UP:            #如果按键向上
                    herospeed = [0,-20]
                if event.key == pygame.K_DOWN:          #如果按键向下
                    herospeed = [0,20]
            if event.type == pygame.KEYUP:
                herospeed = [0, 0]

        pygame.display.flip()                        #更新屏幕

        screen.blit(bg, bgpositon)                   #在屏幕中放置图片    背景图   这个放置顺序有讲究的,得先放背景图再放飞机  要不然飞机被覆盖掉了
        screen.blit(hero, heropostion)               # 参数为: 图片句柄  和  图片的矩形大小
        heropostion = heropostion.move(herospeed)

        screen.blit(bullet, bulletpostion)
        bulletpostion = bulletpostion.move(herospeed)
        clock.tick(10)

if __name__ == '__main__':
    main()











fly.rar

922.01 KB, 下载次数: 5

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-10-26 07:53:06 | 显示全部楼层
python 的库的包集合上传分享一下

site-packages.rar

11.68 MB, 下载次数: 6

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-10-28 21:41:27 | 显示全部楼层
新版的python爬虫包,分享给大家

site-packages.rar

11.96 MB, 下载次数: 5

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-11-28 23:42:49 | 显示全部楼层
这是机器视觉 opencv API 手册 分享给大家

OpenCV官方教程中文版(For Python).rar

5.46 MB, 下载次数: 18

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-12-8 21:59:45 | 显示全部楼层

这是机器视觉 基础练习知识分享给大家

源码笔记.rar

10.15 KB, 下载次数: 7

回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-12-16 19:51:00 | 显示全部楼层
这是深度学习2017最新文档,分享给大家看看。
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-12-16 19:59:20 | 显示全部楼层
深度学习(最全的中文版)_2017年新书.rar  分享给大家学习学习
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2019-12-16 22:33:26 | 显示全部楼层
深度学习(最全的中文版)_2017年新书.rar  分享给大家学习学习11
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-1-1 23:13:31 | 显示全部楼层
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import torch.nn as nn
import matplotlib.pyplot as plt
import numpy as np


class Mydataset(Dataset):
    def __init__(self):
        self.xs = torch.Tensor([[0, 0], [0, 1], [1, 0], [1, 1]])  # 数据
        self.ys = torch.Tensor([[0], [0], [0], [1]])  # 标签

    def __len__(self):
        return len(self.xs)

    def __getitem__(self, index):
        x = self.xs[index]
        y = self.ys[index]
        return x, y


mydataset = Mydataset()
train_data = DataLoader(mydataset, batch_size=4, shuffle=True)


class Mynet(nn.Module):
    def __init__(self):
        super(Mynet, self).__init__()
        self.w = nn.Parameter(torch.randn(2, 1))
        self.b = nn.Parameter(torch.randn(1))

    def forward(self, x):
        return torch.matmul(x, self.w) + self.b


if torch.cuda.is_available():
    net = Mynet().cuda()
else:
    net = Mynet()
loss_fn = nn.MSELoss()
opt = torch.optim.SGD(net.parameters(), lr=0.1)
a1 = []
b1 = []
if __name__=="__main__":
    for epoch in range(200):
        for i, (data_x, data_y) in enumerate(train_data):
            if torch.cuda.is_available():
                xs = data_x.cuda()
                ys = data_y.cuda()
            else:
                xs = data_x
                ys = data_y

            outputs = net(xs)
            # print(outputs)
            loss = loss_fn(outputs, ys)

            opt.zero_grad()
            loss.backward()
            opt.step()

            loss_ = loss.cpu().detach().numpy()
            # print(loss_)
            ouput = outputs.cpu().detach().numpy()

            xs = xs.cpu().detach().numpy()
            ys = ys.cpu().detach().numpy()

            print(ouput, "\n", ys)

        plt.ion()
        plt.clf()
        windows1 = plt.subplot(2, 1, 1)
        a1.append(epoch)
        b1.append(loss_)
        plt.plot(a1, b1)

        windows1 = plt.subplot(2, 1, 2)
        plt.plot(3, 3)
        plt.plot(0, 0, "ob")
        plt.plot(0, 1, "ob")
        plt.plot(1, 0, "ob")
        plt.plot(1, 1, "or")
        point = np.max(ys)
        plt.plot([point + 0.5, 0], [0, point + 0.5], linestyle="--")
        point1 = np.max(outputs.cpu().detach().numpy())
        plt.plot([point1 + 0.5, 0], [0, point1 + 0.5])
        plt.pause(0.1)
        plt.ioff()

    plt.show()
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-1-7 00:01:15 | 显示全部楼层
29 ===============================29

import torch
import matplotlib.pyplot as plt
import numpy as np

x=torch.tensor([5.],requires_grad=True)
print(x)

b=x**2
opt=torch.optim.SGD([x],lr=0.9)

for i in range(1000):
    opt.zero_grad()# 清空梯度
    b.backward(retain_graph=True)
    opt.step()#更新梯度
    print("第{}次".format(i),x)
    plt.ion()
    plt.clf()
    plt.plot(10,10)
    m=np.linspace(-5,5,100)
    plt.plot(m,m**2,color="g")
    plt.plot(x.detach().numpy()[0],x.detach().numpy()[0]**2,"or")

    plt.pause(0.1)
    plt.ioff()
plt.show()
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-1-7 00:01:40 | 显示全部楼层
30 ========================= 30
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets,transforms

batchsize=100
lr=0.01
num_epoch=3

data_transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize([0],[1])])

train_dataset=datasets.MNIST(root="MNIST_data", train=True, transform=data_transform, download=True)
test_dataset=datasets.MNIST(root="MNIST_data", train=False, transform=data_transform, download=False)

# print(train_dataset.train_data.size())
# print(train_dataset.train_labels.size())
#
# print(test_dataset.test_data.size())
# print(test_dataset.test_labels.size())

train_loader=DataLoader(train_dataset,batch_size=batchsize,shuffle=True)
test_loader=DataLoader(test_dataset,batch_size=batchsize,shuffle=False)

class Mynet2(nn.Module):
    def __init__(self,inputs,hidden1,hidden2,output):
        super(Mynet2,self).__init__()
        self.layer=nn.Sequential(nn.Linear(in_features=inputs, out_features=hidden1, bias=True),
                                 nn.ReLU(inplace=True),
                                 nn.Linear(in_features=hidden1, out_features=hidden2, bias=True),
                                 nn.ReLU(inplace=True),
                                 nn.Linear(in_features=hidden2, out_features=output, bias=True),
                                 )
    def forward(self, x):
        y=self.layer(x)
        return y

mynet=Mynet2(784,256,128,10)

if torch.cuda.is_available():
    mynet=mynet.cuda()
else:
    mynet=mynet

loss_fn=nn.CrossEntropyLoss()
opt=torch.optim.SGD(mynet.parameters(),lr=lr)

for epoch in range(num_epoch):
    for i ,(x_s,y_s) in enumerate(train_loader):
        # print(x_s.size())
        # print(x_s.size()[0])

        x_s=x_s.reshape(x_s.size()[0],-1)

        if torch.cuda.is_available():
            x_s1=x_s.cuda()
            y_s1=y_s.cuda()
        else:
            x_s1 = x_s
            y_s1 = y_s

        out=mynet(x_s1)

        loss=loss_fn(out,y_s1)

        opt.zero_grad()
        loss.backward()
        opt.step()

        if i%10==0:
            print("epoch:{},i:{},loss:{:.5}".format(epoch,i,loss.detach()))





回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-1-13 23:31:31 | 显示全部楼层
================================= 31

import torch
from torch.utils.data import DataLoader
from torchvision import datasets,transforms
from PIL import Image
import PIL.ImageDraw as draw
from PIL import ImageFont
import matplotlib.pyplot as plt

from test030 import Mynet2

#mynet=Mynet2(784,256,128,64,10)
net=torch.load("models/mynet.pth")
print(net)

batchsize=1

data_transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize([0],[1])])
test_dataset=datasets.MNIST(root="MNIST_data", train=False, transform=data_transform, download=False)
test_loader=DataLoader(test_dataset,batch_size=batchsize,shuffle=False)

for data in test_loader:
    img,label=data
    # print(img.size())
    # print(label.size())
    img2=img.numpy()[0][0]*255
    #print(img2)
    img2=Image.fromarray(img2)
    img_draw=draw.ImageDraw(img2)

    if torch.cuda.is_available():
        img=img.reshape(img.size()[0],-1)
        img1=img.cuda()
    else:
        img = img.reshape(img.size()[0], -1)
        img1 = img

    out=net(img1)
    # print(out.size())

    maxout=torch.argmax(out,1) # 取1轴最大值索引

    font1=ImageFont.truetype("arial.ttf",size=10)

    img_draw.text(xy=(0,0),text=str(out2),fill=255,font=font1)
    img_draw.text(xy=(22, 0),text=str(label[0].item()), fill=255, font=font1)

    print("网络输出:{},原图的标签:{}".format(out2,label[0].item()),"\n")

    plt.imshow(img2)
    plt.pause(0.5)
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-3-15 22:55:33 | 显示全部楼层
import os
import matplotlib.pyplot as plt
import itertools
import pickle
import imageio
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable

# G(z)
class generator(nn.Module):
    # initializers
    def __init__(self, input_size=32, n_class = 10):
        super(generator, self).__init__()
        self.fc1 = nn.Linear(input_size, 256)
        self.fc2 = nn.Linear(self.fc1.out_features, 512)
        self.fc3 = nn.Linear(self.fc2.out_features, 1024)
        self.fc4 = nn.Linear(self.fc3.out_features, n_class)

    # forward method
    def forward(self, input):
        x = F.leaky_relu(self.fc1(input))
        x = F.leaky_relu(self.fc2(x))
        x = F.leaky_relu(self.fc3(x))
        x = F.tanh(self.fc4(x))

        return x

class discriminator(nn.Module):
    # initializers
    def __init__(self, input_size=32, n_class=10):
        super(discriminator, self).__init__()
        self.fc1 = nn.Linear(input_size, 1024)
        self.fc2 = nn.Linear(self.fc1.out_features, 512)
        self.fc3 = nn.Linear(self.fc2.out_features, 256)
        self.fc4 = nn.Linear(self.fc3.out_features, n_class)

    # forward method
    def forward(self, input):
        x = F.leaky_relu(self.fc1(input), 0.2)
        x = F.dropout(x, 0.3)
        x = F.leaky_relu(self.fc2(x), 0.2)
        x = F.dropout(x, 0.3)
        x = F.leaky_relu(self.fc3(x), 0.2)
        x = F.dropout(x, 0.3)
        x = F.sigmoid(self.fc4(x))

        return x

fixed_z_ = torch.randn((5 * 5, 100))    # fixed noise
fixed_z_ = Variable(fixed_z_.cuda(), volatile=True)
def show_result(num_epoch, show = False, save = False, path = 'result.png', isFix=False):
    z_ = torch.randn((5*5, 100))
    z_ = Variable(z_.cuda(), volatile=True)

    G.eval()
    if isFix:
        test_images = G(fixed_z_)
    else:
        test_images = G(z_)
    G.train()

    size_figure_grid = 5
    fig, ax = plt.subplots(size_figure_grid, size_figure_grid, figsize=(5, 5))
    for i, j in itertools.product(range(size_figure_grid), range(size_figure_grid)):
        ax[i, j].get_xaxis().set_visible(False)
        ax[i, j].get_yaxis().set_visible(False)

    for k in range(5*5):
        i = k // 5
        j = k % 5
        ax[i, j].cla()
        ax[i, j].imshow(test_images[k, :].cpu().data.view(28, 28).numpy(), cmap='gray')

    label = 'Epoch {0}'.format(num_epoch)
    fig.text(0.5, 0.04, label, ha='center')
    plt.savefig(path)

    if show:
        plt.show()
    else:
        plt.close()


# training parameters
batch_size = 128
lr = 0.0002
train_epoch = 100

# data_loader
transform = transforms.Compose([
     transforms.ToTensor(),
     transforms.Lambda(lambda x: x.repeat(3,1,1)),
     transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])
train_loader = torch.utils.data.DataLoader(
    datasets.MNIST('data', train=True, download=True, transform=transform),
    batch_size=batch_size, shuffle=True)

# network
G = generator(input_size=100, n_class=28*28)
D = discriminator(input_size=28*28, n_class=1)
G.cuda()
D.cuda()

# Binary Cross Entropy loss
BCE_loss = nn.BCELoss()

# Adam optimizer
G_optimizer = optim.Adam(G.parameters(), lr=lr)
D_optimizer = optim.Adam(D.parameters(), lr=lr)

# results save folder
if not os.path.isdir('MNIST_GAN_results'):
    os.mkdir('MNIST_GAN_results')
if not os.path.isdir('MNIST_GAN_results/Random_results'):
    os.mkdir('MNIST_GAN_results/Random_results')
if not os.path.isdir('MNIST_GAN_results/Fixed_results'):
    os.mkdir('MNIST_GAN_results/Fixed_results')

train_hist = {}
train_hist['D_losses'] = []
train_hist['G_losses'] = []
for epoch in range(train_epoch):
    D_losses = []
    G_losses = []
    for x_, _ in train_loader:              # 从数据集中导入手写数字
        # train discriminator D
        D.zero_grad()                      # 梯度清零   为什么要梯度清零呢? 训练一个批次要跟另一个批次分开,训练第二次的梯度要重新开始

        x_ = x_.view(-1, 28 * 28)           # 使得输入图片形状统一。虽然导入的图片都是一样的,但是
        # 这里要确保一下输入图片形状,也许下个项目输入图片的形状不一样也不一定

        mini_batch = x_.size()[0]           # 这取得一个批次有多少张图片

        y_real_ = torch.ones(mini_batch)    # 创建真实图片标签 1
        y_fake_ = torch.zeros(mini_batch)   # 创建 假 图片标签 0

        x_, y_real_, y_fake_ = Variable(x_.cuda()), Variable(y_real_.cuda()), Variable(y_fake_.cuda())
        D_result = D(x_)
        D_real_loss = BCE_loss(D_result, y_real_)
        D_real_score = D_result

        z_ = torch.randn((mini_batch, 100))
        z_ = Variable(z_.cuda())
        G_result = G(z_)

        D_result = D(G_result)
        D_fake_loss = BCE_loss(D_result, y_fake_)
        D_fake_score = D_result

        D_train_loss = D_real_loss + D_fake_loss

        D_train_loss.backward()
        D_optimizer.step()

        D_losses.append(D_train_loss.item())

        # train generator G
        G.zero_grad()

        z_ = torch.randn((mini_batch, 100))
        y_ = torch.ones(mini_batch)

        z_, y_ = Variable(z_.cuda()), Variable(y_.cuda())
        G_result = G(z_)
        D_result = D(G_result)
        G_train_loss = BCE_loss(D_result, y_)
        G_train_loss.backward()
        G_optimizer.step()

        G_losses.append(G_train_loss.item())

    print('[%d/%d]: loss_d: %.3f, loss_g: %.3f' % (
        (epoch + 1), train_epoch, torch.mean(torch.FloatTensor(D_losses)), torch.mean(torch.FloatTensor(G_losses))))
    p = 'MNIST_GAN_results/Random_results/MNIST_GAN_' + str(epoch + 1) + '.png'
    fixed_p = 'MNIST_GAN_results/Fixed_results/MNIST_GAN_' + str(epoch + 1) + '.png'
    show_result((epoch+1), save=True, path=p, isFix=False)
    show_result((epoch+1), save=True, path=fixed_p, isFix=True)
    train_hist['D_losses'].append(torch.mean(torch.FloatTensor(D_losses)))
    train_hist['G_losses'].append(torch.mean(torch.FloatTensor(G_losses)))


print("Training finish!... save training results")
torch.save(G.state_dict(), "MNIST_GAN_results/generator_param.pkl")
torch.save(D.state_dict(), "MNIST_GAN_results/discriminator_param.pkl")
with open('MNIST_GAN_results/train_hist.pkl', 'wb') as f:
    pickle.dump(train_hist, f)



images = []
for e in range(train_epoch):
    img_name = 'MNIST_GAN_results/Fixed_results/MNIST_GAN_' + str(e + 1) + '.png'
    images.append(imageio.imread(img_name))
imageio.mimsave('MNIST_GAN_results/generation_animation.gif', images, fps=5)
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-3-18 22:41:48 | 显示全部楼层
import torch.nn as nn
import torch

pool = nn.MaxPool2d(2, stride=2, return_indices=True)
unpool = nn.MaxUnpool2d(2, stride=2)
input = torch.tensor([[[[ 1.,  2,  3,  4],
                            [ 5,  6,  7,  8],
                            [ 9, 10, 11, 12],
                            [13, 14, 15, 16]]]])
output, indices = pool(input)
print(output.shape)
print(indices)
print(unpool(output, indices,output_size=torch.Size([1,1,4,4])))    # 反池化 指定输出形状  output_size=torch.Size([1,1,5,5])

# input=torch.arange(1,5,dtype=torch.float32).reshape(1,1,2,2)
# print(input)

# m=nn.Upsample(scale_factor=3,mode="nearest")
# print(m(input))

# m=nn.Upsample(scale_factor=2,mode="linear",align_corners=False)
# print(m(input.reshape(1,2,2)))
#
# m=nn.Upsample(scale_factor=2,mode="linear",align_corners=True)
# print(m(input.reshape(1,2,2)))
#
# m=nn.Upsample(scale_factor=2,mode="bilinear",align_corners=False)
# print(m(input))
#
# m=nn.Upsample(scale_factor=2,mode="bilinear",align_corners=True)
# print(m(input))
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-3-18 22:42:26 | 显示全部楼层
from torchvision import transforms
from torch import nn
from PIL import Image
import torch
loader=transforms.Compose([transforms.ToTensor()])

unloader=transforms.ToPILImage()

def image_loader(image_name):
    image=Image.open(image_name).convert("RGB")
    image=loader(image).unsqueeze(0)
    return image

def tensor_to_PIL(tesnor):
    image=tesnor
    image=image.squeeze(0)
    image=unloader(image)
    return image

img=image_loader("pic.jpg")
print(img.shape)

#m=nn.Upsample(scale_factor=3,mode="nearest")
# m=nn.Upsample(scale_factor=2,mode="bilinear",align_corners=False)
# m=nn.ConvTranspose2d(3,3,3,2,2,0)
# img=tensor_to_PIL(m(img))
# img.show()

m1=nn.Conv2d(3,27,1)
print(m1(img).shape)
m2=nn.PixelShuffle(3)

img=tensor_to_PIL(m2(m1(img)))
img.show()


回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-4-21 22:51:10 | 显示全部楼层
MTCNN

import os
from PIL import Image
import numpy as np
from tool import utils
import traceback

anno_src = r"D:\cebela\Anno\list_bbox_celeba.txt"
img_dir = r"D:\cebela\img_celeba"

save_path = r"D:\celeba4"

for face_size in [12]:

    print("gen %i image" % face_size)
    # 样本图片存储路径
    positive_image_dir = os.path.join(save_path, str(face_size), "positive")
    negative_image_dir = os.path.join(save_path, str(face_size), "negative")
    part_image_dir = os.path.join(save_path, str(face_size), "part")

    for dir_path in [positive_image_dir, negative_image_dir, part_image_dir]:
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

    # 样本描述存储路径
    positive_anno_filename = os.path.join(save_path, str(face_size), "positive.txt")
    negative_anno_filename = os.path.join(save_path, str(face_size), "negative.txt")
    part_anno_filename = os.path.join(save_path, str(face_size), "part.txt")

    positive_count = 0
    negative_count = 0
    part_count = 0

    try:
        positive_anno_file = open(positive_anno_filename, "w")
        negative_anno_file = open(negative_anno_filename, "w")
        part_anno_file = open(part_anno_filename, "w")

        for i, line in enumerate(open(anno_src)):
            if i < 2:
                continue
            try:
                strs = line.strip().split(" ")
                strs = list(filter(bool, strs))
                image_filename = strs[0].strip()
                print(image_filename)
                image_file = os.path.join(img_dir, image_filename)

                with Image.open(image_file) as img:
                    img_w, img_h = img.size
                    x1 = float(strs[1].strip())
                    y1 = float(strs[2].strip())
                    w = float(strs[3].strip())
                    h = float(strs[4].strip())
                    x2 = float(x1 + w)
                    y2 = float(y1 + h)

                    px1 = 0#float(strs[5].strip())
                    py1 = 0#float(strs[6].strip())
                    px2 = 0#float(strs[7].strip())
                    py2 = 0#float(strs[8].strip())
                    px3 = 0#float(strs[9].strip())
                    py3 = 0#float(strs[10].strip())
                    px4 = 0#float(strs[11].strip())
                    py4 = 0#float(strs[12].strip())
                    px5 = 0#float(strs[13].strip())
                    py5 = 0#float(strs[14].strip())

                    if max(w, h) < 40 or x1 < 0 or y1 < 0 or w < 0 or h < 0:
                        continue

                    boxes = [[x1, y1, x2, y2]]

                    # 计算出人脸中心点位置
                    cx = x1 + w / 2
                    cy = y1 + h / 2

                    # 使正样本和部分样本数量翻倍
                    for _ in range(5):
                        # 让人脸中心点有少许的偏移
                        w_ = np.random.randint(-w * 0.2, w * 0.2)
                        h_ = np.random.randint(-h * 0.2, h * 0.2)
                        cx_ = cx + w_
                        cy_ = cy + h_

                        # 让人脸形成正方形,并且让坐标也有少许的偏离
                        side_len = np.random.randint(int(min(w, h) * 0.8), np.ceil(1.25 * max(w, h)))
                        x1_ = np.max(cx_ - side_len / 2, 0)
                        y1_ = np.max(cy_ - side_len / 2, 0)
                        x2_ = x1_ + side_len
                        y2_ = y1_ + side_len

                        crop_box = np.array([x1_, y1_, x2_, y2_])

                        # 计算坐标的偏移值
                        offset_x1 = (x1 - x1_) / side_len
                        offset_y1 = (y1 - y1_) / side_len
                        offset_x2 = (x2 - x2_) / side_len
                        offset_y2 = (y2 - y2_) / side_len

                        offset_px1 = 0#(px1 - x1_) / side_len
                        offset_py1 = 0#(py1 - y1_) / side_len
                        offset_px2 = 0#(px2 - x1_) / side_len
                        offset_py2 = 0#(py2 - y1_) / side_len
                        offset_px3 = 0#(px3 - x1_) / side_len
                        offset_py3 = 0#(py3 - y1_) / side_len
                        offset_px4 = 0#(px4 - x1_) / side_len
                        offset_py4 = 0#(py4 - y1_) / side_len
                        offset_px5 = 0#(px5 - x1_) / side_len
                        offset_py5 = 0#(py5 - y1_) / side_len

                        # 剪切下图片,并进行大小缩放
                        face_crop = img.crop(crop_box)
                        face_resize = face_crop.resize((face_size, face_size))

                        iou = utils.iou(crop_box, np.array(boxes))[0]
                        if iou > 0.65:  # 正样本
                            positive_anno_file.write(
                                "positive/{0}.jpg {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15}\n".format(
                                    positive_count, 1, offset_x1, offset_y1,
                                    offset_x2, offset_y2, offset_px1, offset_py1, offset_px2, offset_py2, offset_px3,
                                    offset_py3, offset_px4, offset_py4, offset_px5, offset_py5))
                            positive_anno_file.flush()
                            face_resize.save(os.path.join(positive_image_dir, "{0}.jpg".format(positive_count)))
                            positive_count += 1
                        elif iou > 0.4:  # 部分样本
                            part_anno_file.write(
                                "part/{0}.jpg {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15}\n".format(
                                    part_count, 2, offset_x1, offset_y1,offset_x2,
                                    offset_y2, offset_px1, offset_py1, offset_px2, offset_py2, offset_px3,
                                    offset_py3, offset_px4, offset_py4, offset_px5, offset_py5))
                            part_anno_file.flush()
                            face_resize.save(os.path.join(part_image_dir, "{0}.jpg".format(part_count)))
                            part_count += 1
                        elif iou < 0.3:
                            negative_anno_file.write(
                                "negative/{0}.jpg {1} 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n".format(negative_count, 0))
                            negative_anno_file.flush()
                            face_resize.save(os.path.join(negative_image_dir, "{0}.jpg".format(negative_count)))
                            negative_count += 1

                        # 生成负样本
                        _boxes = np.array(boxes)

                    for i in range(5):
                        side_len = np.random.randint(face_size, min(img_w, img_h) / 2)
                        x_ = np.random.randint(0, img_w - side_len)
                        y_ = np.random.randint(0, img_h - side_len)
                        crop_box = np.array([x_, y_, x_ + side_len, y_ + side_len])

                        if np.max(utils.iou(crop_box, _boxes)) < 0.3:
                            face_crop = img.crop(crop_box)
                            face_resize = face_crop.resize((face_size, face_size), Image.ANTIALIAS)

                            negative_anno_file.write("negative/{0}.jpg {1} 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n".format(negative_count, 0))
                            negative_anno_file.flush()
                            face_resize.save(os.path.join(negative_image_dir, "{0}.jpg".format(negative_count)))
                            negative_count += 1
            except Exception as e:
                traceback.print_exc()


    finally:
        positive_anno_file.close()
        negative_anno_file.close()
        part_anno_file.close()
回复 支持 反对

使用道具 举报

2

主题

19

帖子

0

精华

初级会员

Rank: 2

积分
84
金钱
84
注册时间
2020-3-14
在线时间
15 小时
发表于 2020-4-30 10:57:31 | 显示全部楼层
楼主辛苦
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-5-12 22:38:02 | 显示全部楼层
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import numpy as np
import os
import torch.nn.functional as F


class Arcsoftmax(nn.Module):
    def __init__(self, feature_num, cls_num):
        super().__init__()
        self.w = nn.Parameter(torch.randn((feature_num, cls_num)).cuda())

    def forward(self, x, s, m):
        x_norm = F.normalize(x, dim=1)
        w_norm = F.normalize(self.w, dim=0)
        # 将cosa变小,防止acosa梯度爆炸
        cosa = torch.matmul(x_norm, w_norm) / 10
        a = torch.acos(cosa)
        # 这里再乘回来
        arcsoftmax = torch.exp(
            s * torch.cos(a + m) * 10) / (torch.sum(torch.exp(s * cosa * 10), dim=1, keepdim=True) - torch.exp(
            s * cosa * 10) + torch.exp(s * torch.cos(a + m) * 10))


        return arcsoftmax


class ClsNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv_layer = nn.Sequential(nn.Conv2d(1, 32, 3), nn.BatchNorm2d(32), nn.PReLU(),
                                        nn.Conv2d(32, 64, 3), nn.BatchNorm2d(64), nn.PReLU(),
                                        nn.MaxPool2d(3, 2))
        self.feature_layer = nn.Sequential(nn.Linear(11 * 11 * 64, 256), nn.BatchNorm1d(256), nn.PReLU(),
                                           nn.Linear(256, 128), nn.BatchNorm1d(128), nn.PReLU(),
                                           nn.Linear(128, 2), nn.PReLU())
        self.arcsoftmax = Arcsoftmax(2, 10)
        self.loss_fn = nn.NLLLoss()

    def forward(self, x, s, m):
        conv = self.conv_layer(x)
        conv = conv.reshape(x.size(0), -1)
        feature = self.feature_layer(conv)
        out = self.arcsoftmax(feature, s, m)
        out = torch.log(out)
        return feature, out

    def get_loss(self, out, ys):
        return self.loss_fn(out, ys)
        # print(ys*torch.sum(-out, dim=1))
        # exit()
        # return torch.sum(-ys*out)

if __name__ == '__main__':
    weight_save_path = r"./params/arc_loss.pt"
    dataset = datasets.MNIST(root="D:/data/", train=True, transform=transforms.ToTensor(),
                             download=True)
    dataloader = DataLoader(dataset=dataset, batch_size=512, shuffle=True, num_workers=5)
    cls_net = ClsNet()
    cls_net.cuda()
    if os.path.exists(weight_save_path):
        cls_net.load_state_dict(torch.load(weight_save_path))
    fig, ax = plt.subplots()
    for epoch in range(100000):
        for i, (xs, ys) in enumerate(dataloader):
            xs = xs.cuda()
            ys = ys.cuda()
            coordinate, out = cls_net(xs, 1, 1)
            coordinate = coordinate.cpu().detach().numpy()
            loss = cls_net.get_loss(out, ys)
            optimizer = optim.Adam(cls_net.parameters())
            # print([i for i, c in cls_net.named_parameters()])
            # exit()

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            print(loss.cpu().detach().item())
            torch.save(cls_net.state_dict(), weight_save_path)
            print("save success")
            ys = ys.cpu().numpy()
            plt.cla()
            plt.ion()
            for j in np.unique(ys):
                xx = coordinate[ys == j][:, 0]
                yy = coordinate[ys == j][:, 1]
                ax.scatter(xx, yy)
            # if i % 1000 == 0:
            #     fig.savefig(r"D:/center_loss.jpg")
            plt.show()
            plt.pause(0.1)
            plt.ioff()
回复 支持 反对

使用道具 举报

2

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
115
金钱
115
注册时间
2019-2-15
在线时间
18 小时
 楼主| 发表于 2020-5-12 22:38:27 | 显示全部楼层
import torch.nn as nn
import torch
import torch.utils.data as data
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import os


save_path = "models/net.pth"

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Sequential(
            nn.Linear(784,512),
            nn.BatchNorm1d(512),
            nn.ReLU(),
            nn.Linear(512,256),
            nn.BatchNorm1d(256),
            nn.ReLU(),
            nn.Linear(256,128),
            nn.BatchNorm1d(128),
            nn.ReLU(),
            nn.Linear(128, 2)
        )
        self.fc2 = nn.Sequential(
            nn.Linear(2, 10),
            nn.Softmax()
        )
    def forward(self, x):
        fc1_out = self.fc1(x)
        out = self.fc2(fc1_out)
        return out,fc1_out

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_data = torchvision.datasets.MNIST(root="MNIST",download=True,train=True,transform=transforms.Compose([transforms.ToTensor()]))
train_loader = data.DataLoader(dataset=train_data,shuffle=True,batch_size=512)

def visualize(feat, labels, epoch):
    plt.ion()
    c = ['#ff0000', '#ffff00', '#00ff00', '#00ffff', '#0000ff',
         '#ff00ff', '#990000', '#999900', '#009900', '#009999']
    plt.clf()
    for i in range(10):
        plt.plot(feat[labels == i, 0], feat[labels == i, 1], '.', c=c[i])
    plt.legend(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], loc = 'upper right')
    # plt.xlim(xmin=-100,xmax=100)
    # plt.ylim(ymin=-100,ymax=100)
    plt.title("epoch=%d" % epoch)
    plt.savefig('./images/epoch=%d.jpg' % epoch)
    plt.draw()
    plt.pause(0.001)

if __name__ == '__main__':
    net = Net().to(device)
    if os.path.exists(save_path):
        net = torch.load(save_path)
    loss_fun = nn.MSELoss().to(device)
    optimzer = torch.optim.Adam(net.parameters())


    epoch = 0
    while True:
        feat_loader = []
        label_loader = []
        for i, (x, y) in enumerate(train_loader):
            x = x.view(-1,784).to(device)
            target = torch.zeros(y.size(0), 10).scatter_(1, y.view(-1, 1), 1).to(device)
            # target = y.to(device)
            out_put,feat = net(x)
            loss = loss_fun(out_put,target)

            optimzer.zero_grad()
            loss.backward()
            optimzer.step()

            feat_loader.append(feat)
            label_loader.append((y))


            if i % 10 == 0:
                print(loss.item())
        feat = torch.cat(feat_loader, 0)
        labels = torch.cat(label_loader, 0)
        visualize(feat.data.cpu().numpy(), labels.data.cpu().numpy(), epoch)
        epoch+=1
        torch.save(net, save_path)

回复 支持 反对

使用道具 举报

1

主题

7

帖子

0

精华

新手上路

积分
25
金钱
25
注册时间
2018-12-27
在线时间
3 小时
发表于 2020-7-12 23:07:24 | 显示全部楼层
import torch
from torch.utils.data import Dataset
import torchvision
import numpy as np
import cfg
import os
from skimage.transform import resize
from PIL import Image
import math
import dataset
import glob
import time

LABEL_FILE_PATH = "data/person_label.txt"
IMG_BASE_DIR = "data"
IMG_PATH = "data/test"
transforms = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor()
])


def one_hot(cls_num, v):
    b = np.zeros(cls_num)
    b[v] = 1.
    return b

class TrainDataset(Dataset):
    def __init__(self, img_size=416):
        self.files = sorted(glob.glob('%s/*.jpg*' % IMG_PATH))
        # print(self.files) # ['data/test\\1.jpg', 'data/test\\2.jpg', 'data/test\\3.jpg', 'data/test\\4.jpg']
        self.img_shape = (img_size, img_size)
        with open(LABEL_FILE_PATH) as f:
            self.dataset = f.readlines()        #  打开标签文件  然后 按行读取

    def __len__(self):
        return len(self.files)

    def __getitem__(self, index):
        img_path = self.files[index % len(self.files)]
        # 一张一张的去处理
        # Extract image
        img = np.array(Image.open(img_path))
        h, w, _ = img.shape
        dim_diff = np.abs(h - w)
        # Upper (left) and lower (right) padding
        pad1, pad2 = dim_diff // 2, dim_diff - dim_diff // 2
        # Determine padding
        pad = ((pad1, pad2), (0, 0), (0, 0)) if h <= w else ((0, 0), (pad1, pad2), (0, 0))
        # Add padding
        input_img = np.pad(img, pad, 'constant', constant_values=127.5) / 255.
        # Resize and normalize
        input_img = resize(input_img, (*self.img_shape, 3), mode='reflect')
        # 将图片放缩至数据集规定的尺寸, 同时进行归一化操作

        # Channels-first
        input_img = np.transpose(input_img, (2, 0, 1))
        # As pytorch tensor
        input_img = torch.from_numpy(input_img).float()

        # -------------------------
        #        Label
        # -------------------------
        labels = {}
        line = self.dataset[index]              # 按照索引来取行
        print(line)
        strs = line.split()                     # 分割标签 每行按照空格来分割
        a = os.path.join(IMG_BASE_DIR, strs[0])

        _img_data = Image.open(os.path.join(IMG_BASE_DIR, strs[0]))     #第 0 个元素是图片的名字 然后 Image.open 图片绝对路径

        img_data = transforms(_img_data)                #
        # print(img_data.shape)
        # print(strs[2])
        _boxes = np.array([float(x) for x in strs[1:]])         # 原来strs是list的 这条把strs 变成了numpy
        # print("__box: ",type(_boxes[0]))
        # _boxes = np.array(list(map(float, strs[1:])))
        boxes = np.split(_boxes, len(_boxes) // 5)
        # print("adasdasd",type(boxes[0]))
        # print("boxes: ",boxes)
        for feature_size, anchors in cfg.ANCHORS_GROUP.items():     # feature_size = 13  26  52   anchors = 建议框的宽高
            print("feature_size: ",feature_size)
            print("anchors: ",anchors)
            labels[feature_size] = np.zeros(shape=(feature_size, feature_size, 3, 5 + cfg.CLASS_NUM))
            # print("labels[feature_size]: ",labels[feature_size].shape)
            for box in boxes:
                cls, cx, cy, w, h = box #  box是标签分离出来的东西   标签为:分类,中心点xy坐标,真实框宽高

                cx_offset, cx_index = math.modf(cx * feature_size / cfg.IMG_WIDTH)
                # print("cx_offset, cx_index ",cx_offset, cx_index )      # =========  x
                # cx_offset 是中心点相对于特征图中偏移量的 小数部分
                # cx_index 整数部分
                cy_offset, cy_index = math.modf(cy * feature_size / cfg.IMG_WIDTH)  # =========  y

                for i, anchor in enumerate(anchors):
                    anchor_area = cfg.ANCHORS_GROUP_AREA[feature_size][i]   # feature_size 是13 26 52 ,它这里是按建议框尺寸来遍历的
                    #返回的是建议框的面积
                    p_w, p_h = w / anchor[0], h / anchor[1]     # 实际框宽高/ 建议框宽高。==== 求出偏移量
                    p_area = w * h  # 实际框的面积
                    iou = min(p_area, anchor_area) / max(p_area, anchor_area)       # 计算iou 这里是最小面积除以最大面积
                    #
    #                 labels[feature_size][int(cy_index), int(cx_index), i] = np.array(
    #                     [iou, cx_offset, cy_offset, np.log(p_w), np.log(p_h), *one_hot(cfg.CLASS_NUM, int(cls))])
    # # *one_hot(cfg.CLASS_NUM, int(cls)) 这里是分类 CLASS_NUM 是 10 分类,所以 cls 是 2 的时候,把它用onehot表现出来
    #     print("ok====")
    #     return labels[13], labels[26], labels[52], img_data     # 所有的目标 * 9 个框(3个形状,3个尺寸)
        return  1

# ==============================================================================================


class TestMyDataset(Dataset):
    def __init__(self,img_size=416):
        self.files = sorted(glob.glob('%s/*.jpg*' % IMG_PATH))
        print(self.files) # ['data/test\\1.jpg', 'data/test\\2.jpg', 'data/test\\3.jpg', 'data/test\\4.jpg']
        self.img_shape = (img_size, img_size)
        #
        # img = Image.open(self.files[0])
        # img.show()
        # print("=======")
        # for filename in os.listdir(IMG_PATH):
        #    print(filename)

    def __getitem__(self, index):
        img_path = self.files[index % len(self.files)]
        # print(index)
        # print(img_path)
        # 0
        # data / test\1.jpg

        # 1
        # data / test\2.jpg
        #一张一张的去处理

        # Extract image
        img = np.array(Image.open(img_path))
        h, w, _ = img.shape
        dim_diff = np.abs(h - w)
        # Upper (left) and lower (right) padding
        pad1, pad2 = dim_diff // 2, dim_diff - dim_diff // 2
        # Determine padding
        pad = ((pad1, pad2), (0, 0), (0, 0)) if h <= w else ((0, 0), (pad1, pad2), (0, 0))
        # Add padding
        input_img = np.pad(img, pad, 'constant', constant_values=127.5) / 255.
        # Resize and normalize
        input_img = resize(input_img, (*self.img_shape, 3), mode='reflect')
        # 将图片放缩至数据集规定的尺寸, 同时进行归一化操作

        # Channels-first
        input_img = np.transpose(input_img, (2, 0, 1))
        # As pytorch tensor
        input_img = torch.from_numpy(input_img).float()

        return img_path, input_img

    def __len__(self):
        return len(self.files)


if __name__ == '__main__':

    myDataset = dataset.TrainDataset()     # 返回 return labels[13], labels[26], labels[52], img_data
    train_loader = torch.utils.data.DataLoader(myDataset, batch_size=2, shuffle=False)

    for batch_i, (img_paths, input_imgs) in enumerate(train_loader):
        # print("input_imgs.shape",input_imgs.shape) # torch.Size([2, 3, 416, 416])
        print("batch_i",batch_i)

        # __getitem__(self, index):  上面的  N C H W  所以在 TestMyDataset 里面的 __getitem__ 里的index 是这样的
        # 虽然输入的是两张图片,但是他会一张一张的去处理
        #

        pass
回复 支持 反对

使用道具 举报

1

主题

7

帖子

0

精华

新手上路

积分
25
金钱
25
注册时间
2018-12-27
在线时间
3 小时
发表于 2020-7-17 19:36:23 | 显示全部楼层

def non_max_suppression(prediction, num_classes, conf_thres=0.5, nms_thres=0.4):
    """
    Removes detections with lower object confidence score than 'conf_thres' and performs
    Non-Maximum Suppression to further filter detections.
    Returns detections with shape:
        (x1, y1, x2, y2, object_conf, class_score, class_pred)
    """
    # object_conf   置信度
    # class_score   各个类别的得分
    # From (center x, center y, width, height) to (x1, y1, x2, y2)
    box_corner = prediction.new(prediction.shape)
    box_corner[:, :, 0] = prediction[:, :, 0] - prediction[:, :, 2] / 2
    box_corner[:, :, 1] = prediction[:, :, 1] - prediction[:, :, 3] / 2
    box_corner[:, :, 2] = prediction[:, :, 0] + prediction[:, :, 2] / 2
    box_corner[:, :, 3] = prediction[:, :, 1] + prediction[:, :, 3] / 2
    prediction[:, :, :4] = box_corner[:, :, :4]

    output = [None for _ in range(len(prediction))]
    for image_i, image_pred in enumerate(prediction):
        # Filter out confidence scores below threshold
        conf_mask = (image_pred[:, 4] >= conf_thres).squeeze()
        image_pred = image_pred[conf_mask]
        # If none are remaining => process next image
        if not image_pred.size(0):
            continue
        # Get score and class with highest confidence
        class_conf, class_pred = torch.max(image_pred[:, 5: 5 + num_classes], 1, keepdim=True)
        # image_pred[:, 5 : 5 + num_classes] 这个就是分类的类别 80个数
        # print("class_conf", class_conf)  # 每个框分类得分最高的 分数
        # print("class_pred", class_pred)  # 每个框分类得分最高的 索引

        # 8 个框里面,置信度的值最高的值的索引:class_pred
        # Detections ordered as (x1, y1, x2, y2, obj_conf, class_conf, class_pred)
        # =============================================================================
        #
        # =============================================================================
        detections = torch.cat((image_pred[:, :5], class_conf.float(), class_pred.float()), 1)
        # image_pred[:, :5] = (x1, y1, x2, y2, object_conf,
        # 7 个数值: 4个坐标  +  框的 iou + 分类最高分 + 最高分的索引
        # print("detections",detections.shape)  # detections torch.Size([8, 7])
        #  取出 8 个框的  坐标和 iou
        # print("detections", detections)
        # Iterate through all predicted classes
        unique_labels = detections[:, -1].cpu().unique()
        # unique_labels 这个是 iou   取 detections 倒数第一个数 然后再去重 然后在从小到大排列
        # unique():返回参数数组中所有不同的值,并按照从小到大排序
        # print("unique_labels", unique_labels)
        if prediction.is_cuda:
            unique_labels = unique_labels.cuda()
        for c in unique_labels:  # 开始循环 类
            # Get the detections with the particular class
            detections_class = detections[detections[:, -1] == c]
            # print("detections_class", detections[:, -1] == c)
            # 输出: detections_class tensor([False, True, False, False, True, True, True, False])
            # 这也是一种掩码之类的。
            # print("ppppppppp", detections_class[:, 4])
            # detections_class 这个就是取到了同一类的框,detections_class[:, 4] 是取他们的 iou置信度(索引为 4 的元素) !!!!!
            # Sort the detections by maximum objectness confidence
            _, conf_sort_index = torch.sort(detections_class[:, 4], descending=True)  # descending=True 是从大到小的意思
            # torch.sort 对 detections_class[:, 4] 4索引也就是第5个数的排序,返回从大到小排序,索引:conf_sort_index
            # 4索引 是iou
            # print("detections_class", conf_sort_index)  # detections_class tensor([2, 1, 3, 0])
            # 因为 detections_class 来承接了同一类的这些框,所以 就变成了一个新的数组,conf_sort_index 就是新的数组 按照框置信度iou从小到大排列顺序
            detections_class = detections_class[conf_sort_index]
            # print("after iou ", detections_class)  # 打印出来之后,得到了新的排序之后的新的 框,重新赋给 detections_class
            # Perform non-maximum suppression
            max_detections = []

            while detections_class.size(0):  # detections_class.size(0) 一次有几个框  torch.Size([4, 7]) torch.Size([2, 7])
                # Get detection with highest confidence and save as max detection
                max_detections.append(detections_class[0].unsqueeze(0))  # 拿到 detections_class 数组置信度最高的
                # detections_class[0] 因为从大到小排序的
                # print("max_detections", max_detections)
                # print("max_detections[-1]", max_detections[-1])
                # Stop if we're at the last detection
                if len(detections_class) == 1:
                    break
                # Get the IOUs for all boxes with lower confidence
                ious = bbox_iou(max_detections[-1], detections_class[1:])
                # print("ious   ", ious)
                # 框置信度最大的跟 其他框做iou
                # Remove detections with IoU >= NMS threshold
                detections_class = detections_class[1:][ious < nms_thres]  # 这个也是掩码的选择操作
                # detections_class 这个是 取 符合 [ious < nms_thres]  条件的  detections_class[1:] 之后的数组
            max_detections = torch.cat(max_detections).data
            # Add max detections to outputs
            # print("output[image_i]", output[image_i])
            output[image_i] = (
                max_detections if output[image_i] is None else torch.cat((output[image_i], max_detections))
                # 如果 output[image_i] 是 None  output[image_i] 就等于 max_detections 如果不是None 那就
                #     output[image_i], max_detections)这两个堆叠起来 ,这个行代码的作用是要把所有的框堆叠起来
                # 但是第一次进来的时候是空的,所以要  max_detections if output[image_i] is None
            )
    print("output.shape", output)
    return output

回复 支持 反对

使用道具 举报

1

主题

7

帖子

0

精华

新手上路

积分
25
金钱
25
注册时间
2018-12-27
在线时间
3 小时
发表于 2020-7-18 17:33:21 | 显示全部楼层
# import os
#
# orh_path = r"/Users/sw/Desktop/111/"
#
# car = r"/Users/sw/Desktop/111/car"
# car_label = r"/Users/sw/Desktop/111/car_label"
#
# run_car = r"/Users/sw/Desktop/111/run_car"
# run_car_new = r"/Users/sw/Desktop/111/run_car_new"
#
# suv = r"/Users/sw/Desktop/111/suv"
# suv_new = r"/Users/sw/Desktop/111/suv_new"
#
# trucks = r"/Users/sw/Desktop/111/trucks"
# trucks_new = r"/Users/sw/Desktop/111/trucks_new"
#
#
# file = os.listdir(trucks_new)
#
# i = 0
#
# for f in file:
#     print(f)
#     if f.endswith(".jpg"):
#         # print("adasdas")
#         old_file = os.path.join(trucks_new, f)
#         new_file = os.path.join(trucks, "{}.jpg".format(i))
#
#         os.rename(old_file, new_file)
#
#         # else:
#         #     os.remove(os.path.join(run_car, f))
#         #     print("adasda")
#     i = i + 1

# import  xml.dom.minidom
# import os
# car_label =r"/Users/sw/Desktop/111/label/car/outputs"
#
# dom = xml.dom.minidom.parse('/Users/sw/Desktop/111/label/car/outputs/1.xml')
#
# root = dom.documentElement
# print (root.nodeName)
# print (root.nodeValue)
# print (root.nodeType)


# -*- coding:utf-8 _*-
"""
@author:xxx
@file: read_json.py
@time: 2019/06/05
"""
# 根据标注精灵标好导出的json文件生成txt
import json
import os
import glob
from tqdm import tqdm

def foo(x):
    if type(x) == str:
        return float(x)
    return x;


def get_files(path, _ends=['*.json']):
    all_files = []
    for _end in _ends:
        files = glob.glob(os.path.join(path, _end))
        all_files.extend(files)
    file_num = len(all_files)
    return all_files, file_num

json_path = r'/Users/sw/Desktop/111/label/test_in/outputs'

files = glob.glob(os.path.join(json_path,'*.json'))

for file in files:
    jason_file = open(file,'r',encoding='utf-8')
    result_dict = json.load(jason_file)
    over_list = []
    path1 = result_dict['path'] # <class 'dict'> result_dict 是字典形式 ======>>> /Users/sw/Desktop/111/1111/car/9.jpg
    # print(result_dict['outputs']['object']) # [{'name': '1', 'bndbox': {'xmin': 18, 'ymin': 11, 'xmax': 514, 'ymax': 296}}]
    # {'path': '/Users/sw/Desktop/111/1111/car/9.jpg','outputs': {'object': [{'name': '1', 'bndbox': {'xmin': 18, 'ymin': 11, 'xmax': 514, 'ymax': 296}}]},

    # 字典的索引就是这么牛
    txt = result_dict['outputs']['object']
    for obj_item in txt:
        coords = obj_item['bndbox']
        try:
            text = obj_item['name']
            output_coord = [path1, int(float(text)),int(float(coords['xmin'])), int(float(coords['ymin'])), int(float(coords['xmax']))
                , int(float(coords['ymax']))]
        except:
                continue
        for fileName2 in output_coord:
            
            over_list.append(fileName2)
        print(over_list)   
        # output_coord.replace("'","")
        # print(output_coord)

        

    # for f in result_dict:
    #  print(f['name'])      
# files = os.open(json_path,)


#
# def get_text_mark(file_path):
#     with open(file_path, 'r', encoding='utf-8') as fid:
#         result_dict = json.load(fid)
#         obj = result_dict['outputs']['object']
#         all_text_mark = []
#         for obj_item in obj:
#             text = obj_item['name']
#             try:
#                 coords = obj_item['polygon']
#                 try:
#                     output_coord = [int(float(coords['x1'])), int(float(coords['y1'])), int(float(coords['x2']))
#                         , int(float(coords['y2'])), int(float(coords['x3'])), int(float(coords['y3'])),
#                                     int(float(coords['x4'])), int(float(coords['y4']))]
#                 except:
#                     continue
#             except:
#                 coords = obj_item['bndbox']
#                 try:
#                     output_coord = [int(float(coords['xmin'])), int(float(coords['ymin'])), int(float(coords['xmax']))
#                         , int(float(coords['ymin'])), int(float(coords['xmax'])), int(float(coords['ymax'])),
#                                     int(float(coords['xmin'])), int(float(coords['ymax']))]
#                 except:
#                     continue
#             output_text = ''
#             for item in output_coord:
#                 output_text = output_text + str(item) + ','
#             output_text += text
#             all_text_mark.append(output_text)
#         return all_text_mark
#
#
# def write_to_txt(out_txt_path, one_file_all_mark):
#     # windows
#     with open(os.path.join(out_txt_path, file.split('\\')
#                                          [-1].split('.')[0] + '.txt'), 'a+', encoding='utf-8') as fid:
#         ##linux
#         # with open(os.path.join(out_txt_path, file.split('/')
#         #                                      [-1].split('.')[0] + '.txt'), 'a+', encoding='utf-8') as fid:
#         for item in one_file_all_mark:
#             fid.write(item + '\n')
#
#
# # car_label =r"/Users/sw/Desktop/111/label/car/outputs"
# #
# if __name__ == "__main__":
#     json_path = r'/Users/sw/Desktop/111/label/test_in/outputs'
#     out_txt_path = r'/Users/sw/Desktop/111/label/test_out'
#     files, files_len = get_files(json_path)
#     bar = tqdm(total=files_len)
#     print("bar",bar)
#     for file in files:
#         bar.update(1)
#         # print(file)
#         try:
#             one_file_all_mark = get_text_mark(file)
#         except:
#             # print(file)
#             continue
#         write_to_txt(out_txt_path, one_file_all_mark)
#     bar.close()
#

# from tqdm import tqdm
# import time
#
# for i in tqdm(range(1000)):
#      #do something
#      time.sleep(0.1)
#      pass


















回复 支持 反对

使用道具 举报

1

主题

7

帖子

0

精华

新手上路

积分
25
金钱
25
注册时间
2018-12-27
在线时间
3 小时
发表于 2020-8-2 23:40:12 | 显示全部楼层
from torchvision import transforms
from torch import nn
from PIL import Image
import torch
loader=transforms.Compose([transforms.ToTensor()])

unloader=transforms.ToPILImage()

def image_loader(image_name):
    image=Image.open(image_name).convert("RGB")
    image=loader(image).unsqueeze(0)
    return image

def tensor_to_PIL(tesnor):
    image=tesnor
    image=image.squeeze(0)
    image=unloader(image)
    return image

img=image_loader("pic.jpg")
print(img.shape)

#m=nn.Upsample(scale_factor=3,mode="nearest")
# m=nn.Upsample(scale_factor=2,mode="bilinear",align_corners=False)
# m=nn.ConvTranspose2d(3,3,3,2,2,0)
# img=tensor_to_PIL(m(img))
# img.show()

m1=nn.Conv2d(3,27,1)
print(m1(img).shape)
m2=nn.PixelShuffle(3)

img=tensor_to_PIL(m2(m1(img)))
img.show()


上采样
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-4-30 16:05

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表