小编Anu*_*ngh的帖子

PyTorch:预期输入 batch_size (12) 匹配目标 batch_size (64)

我尝试了 PyTorch 并想为 MNIST 编写一个程序。但是,我收到了错误消息:

预期输入 batch_size (12) 匹配目标 batch_size (64)

我搜索了一个解决方案,但我不明白我的代码有什么问题。

#kwargs is empty because I don't use cuda
kwargs = {}
train_data = torch.utils.data.DataLoader(
    datasets.MNIST('data', train=True, download=True,
                    transform=transforms.Compose([transforms.ToTensor(),
                    transforms.Normalize((0.1307,),(0.3081,))])),
    batch_size=64, shuffle=True, **kwargs)

test_data = torch.utils.data.DataLoader(
    datasets.MNIST('data', train=False,
                    transform=transforms.Compose([transforms.ToTensor(),
                    transforms.Normalize((0.1307,),(0.3081,))])),
    batch_size=64, shuffle=True, **kwargs)

class Netz(nn.Module):
    def __init__(self):
        super(Netz, self).__init__()
        self.conv1 = nn.Conv2d(1,10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv_dropout = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 60)
        self.fc2 = nn.Linear(60, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.max_pool2d(x, 2) …
Run Code Online (Sandbox Code Playgroud)

python pycharm pytorch

2
推荐指数
1
解决办法
8058
查看次数

ValueError: ('无法解释初始化标识符:', 0.2)

回溯(最近一次调用最后一次):文件“AutoFC_AlexNet_randomsearch_CalTech101_v2.py”,第 112 行,X = rows.Dense(神经元,activation=activation,kernel_initializer=weight_init)(X)文件“/home/shabbeer/NAS/lib/python3 .5/site-packages/keras/legacy/interfaces.py”,第 91 行,在包装器中返回 func(*args, **kwargs) 文件“/home/shabbeer/NAS/lib/python3.5/site-packages/ keras/layers/core.py”,第 824 行,在init self.kernel_initializer =initializers.get(kernel_initializer) 文件“/home/shabbeer/NAS/lib/python3.5/site-packages/keras/initializers.py”中,第 503 行,在获取标识符中)ValueError: ('无法解释初始值设定项标识符:', 0.2)

使用tensorflow-gpu版本1.4.0和keras版本2.1.3运行代码时出现上述错误

python-3.5 keras tensorflow

1
推荐指数
1
解决办法
6045
查看次数

如何在训练 CNN 时为 nn.Linear 层选择参数?

我正在尝试训练 CNNFashion-MNIST使用Conv2dMaxpoolLinear层对数据中的图像进行分类。我in_features = 12*4*4nn.Linear层中遇到了如下所述的代码。

我能否获得有关如何in_features为 nn.Linear 层选择参数的帮助?

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=5)

        self.fc1 = nn.Linear(in_features=12*4*4, out_features=120)
        self.fc2 = nn.Linear(in_features=120, out_features=60)
        self.out = nn.Linear(in_features=60, out_features=10)
Run Code Online (Sandbox Code Playgroud)

python-3.x conv-neural-network pytorch

1
推荐指数
1
解决办法
3711
查看次数

如何取出aa预训练keras模型的中间层

我想使用VGG模型(tensorflow或keras预训练模型)作为特征提取器;我加载VGG16 model

IMG_SHAPE = (224, 224, 3)
vgg16 = tf.keras.applications.VGG16(input_shape = IMG_SHAPE,
                                    include_top=False,
                                    weights='imagenet')
Run Code Online (Sandbox Code Playgroud)

现在如果我有一批图像

image_batch  =np.ones((5,224,224,3),np.float32)
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式获得 VGG16 的最后一层

last_layer = vgg16(image_batch)
Run Code Online (Sandbox Code Playgroud)

有谁知道在给定输入图像 image_batch 的情况下获取中间层特征?也就是说,我想提取给定图像的较低级别特征。非常感谢!

python deep-learning keras tensorflow

1
推荐指数
1
解决办法
3238
查看次数

将叶子与背景分开

我有一组图像,所有这些图像看起来几乎都像这里的这片叶子:

低分辨率图像...

我想从背景中提取叶子,为此我使用了此处GrabCut使用的算法。

作为一种不同的方法,我还使用基于 r、g 和 b 值比率的阈值,如下所示:

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

testImg = cv2.imread('path_to_the_image')
testImg = cv2.resize(testImg, (256, 256))
#bgImg = cv2.imread('')
#blurBg = cv2.GaussianBlur(bgImg, (5, 5), 0)
#blurBg = cv2.resize(blurBg, (256, 256))

#testImg = cv2.GaussianBlur(testImg, (5, 5), 0)
cv2.imshow('testImg', testImg)
#plt.imshow(bgImg)
cv2.waitKey(0)
#plt.show()

modiImg = testImg.copy()    
ht, wd = modiImg.shape[:2]

print(modiImg[0][0][0])

for i in range(ht):
    for j in range(wd):
        r = modiImg[i][j][0]
        g = modiImg[i][j][1]
        b = modiImg[i][j][2]

        r1 = …
Run Code Online (Sandbox Code Playgroud)

python opencv image-processing python-3.x background-subtraction

1
推荐指数
1
解决办法
2111
查看次数