{"id":337,"date":"2022-05-07T09:00:01","date_gmt":"2022-05-07T00:00:01","guid":{"rendered":"https:\/\/is-ai.jp\/?p=337"},"modified":"2022-05-05T17:21:32","modified_gmt":"2022-05-05T08:21:32","slug":"cnntorch-vision%e3%81%ae%e6%a7%8b%e7%af%89%e6%b8%88%e3%81%bf%e3%83%a2%e3%83%87%e3%83%ab%e3%81%ab%e3%82%88%e3%82%8b%e5%9b%9e%e5%b8%b0","status":"publish","type":"post","link":"https:\/\/is-ai.jp\/?p=337","title":{"rendered":"CNN(torch vision\u306e\u69cb\u7bc9\u6e08\u307f\u30e2\u30c7\u30eb)\u306b\u3088\u308b\u56de\u5e30"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u306f\u3058\u3081\u306b<\/h2>\n\n\n\n<p>\u753b\u50cf\u306b\u3088\u308b\u56de\u5e30\u3001\u4f8b\u3048\u3070\u30e9\u30fc\u30e1\u30f3\u306e\u753b\u50cf\u304b\u3089\u98df\u3079\u30ed\u30b0\u8a55\u4fa1\u3092\u4e88\u6e2c\u3057\u305f\u308a\u3001\u4eba\u306e\u9854\u3092\u70b9\u6570\u5316\u3057\u305f\u308a\u306a\u3069\u69d8\u3005\u306a\u7528\u9014\u304c\u8003\u3048\u3089\u308c\u307e\u3059\u304c\u3001\u65e5\u672c\u8a9e\u3067\u3082\u82f1\u8a9e\u3067\u3082\u8cc7\u6599\u304c\u5c11\u306a\u304f\u3001\u4f55\u5ea6\u304b\u8e93\u3044\u305f\u306e\u3067\u5099\u5fd8\u9332\u3068\u3057\u3066\u307e\u3068\u3081\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30e2\u30c7\u30eb\u306ftorchvision\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u304b\u3089\u3001\u65e2\u5b58\u306e\u30af\u30e9\u30b9\u5206\u985e\u306e\u30e2\u30c7\u30eb\u304b\u3089\u5f04\u308b\u3060\u3051\u3067\u52d5\u304f\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u53c2\u8003\u30b5\u30a4\u30c8<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><a href=\"https:\/\/pytorch.org\/tutorials\/beginner\/finetuning_torchvision_models_tutorial.html\">torchvison\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb<\/a><br>https:\/\/medium.com\/@benjamin.phillips22\/simple-regression-with-neural-networks-in-pytorch-313f06910379<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u30af\u30e9\u30b9\u5206\u985e\u304b\u3089\u306e\u5909\u66f4\u70b9<\/h2>\n\n\n\n<p>\u57fa\u672c\u7684\u306b\u753b\u50cf\u306b\u3088\u308b\u56de\u5e30\u306f\u3001\u30af\u30e9\u30b9\u5206\u985e\u306e\u6a5f\u69cb\u3092\u307b\u3068\u3093\u3069\u305d\u306e\u307e\u307e\u6d41\u7528\u3067\u304d\u307e\u3059\u3002<br>\u5fc5\u8981\u306a\u5909\u66f4\u70b9\u306f\u4ee5\u4e0b\u306e\uff13\u70b9\u3067\u3059\u3002<\/p>\n\n\n\n<ul><li>Loss<\/li><li>\u51fa\u529b\u5c64<\/li><li>\u6b63\u89e3\u30c7\u30fc\u30bf\u306e\u4ed8\u4e0e<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Loss<\/h3>\n\n\n\n<p>\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3067\u306fLoss\u304cCrossEntropy\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001MSE\u3084SmoothL1\u306a\u3069\u306e\u56de\u5e30\u306b\u7528\u3044\u3089\u308c\u308bLoss\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Setup the loss fxn\ncriterion = nn.MSELoss()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u51fa\u529b\u5c64<\/h3>\n\n\n\n<p>\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u30d9\u30fc\u30b9\u306b\u5f04\u308a\u305f\u3044\u306e\u3067\u3001\u30af\u30e9\u30b9\u6570\u3092\uff11\u306b\u8a2d\u5b9a\u3059\u308b\u201d\u3060\u3051\u201d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Number of classes in the dataset\nnum_classes = 1<\/code><\/pre>\n\n\n\n<p>\u305f\u3060\u3057\u4e0a\u8a18\u306e\u5909\u66f4\u3060\u3051\u3060\u3068\u51fa\u529b\u5c64\u306e\u30ce\u30fc\u30c9\u304c1024 -&gt; 1 \u306e\u3088\u3046\u306b\u304b\u306a\u308a\u6025\u6fc0\u306a\u6e1b\u5c11\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u305f\u3081\u3001model\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002(ResNet\u306b\u304a\u3051\u308b\u4e00\u4f8b)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model_ft.fc = nn.Sequential(nn.Linear(num_ftrs, 256),\n              nn.LeakyReLU(),\n              nn.Linear(256, 32),\n              nn.LeakyReLU(),\n              nn.Linear(32, 1))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6b63\u89e3\u30c7\u30fc\u30bf\u306e\u4ed8\u4e0e<\/h3>\n\n\n\n<p>pytorch\u306eDataset\u3068\u3044\u3046\u4fbf\u5229\u3059\u304e\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u304a\u304b\u3052\u3067\u3001\u5b66\u7fd2\u30c7\u30fc\u30bf\u3092\u30d5\u30a9\u30eb\u30c0\u3054\u3068\u306b\u5206\u3051\u3066\u304a\u3051\u3070\u52dd\u624b\u306b\u30af\u30e9\u30b9\u306e\u30e9\u30d9\u30eb\u3092\u8cbc\u3063\u3066\u304f\u308c\u307e\u3059\u3002<br>\u3057\u304b\u3057\u3001\u4eca\u56de\u884c\u3044\u305f\u3044\u306e\u306f\u56de\u5e30\u306a\u306e\u3067float\u306a\u308aint\u306a\u308a\u306e\u6570\u5b57\u3092\u6301\u305f\u305b\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3001Dataset\u3092\u81ea\u4f5c\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>*pytorch\u306e\u81ea\u4f5c\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306f\u7d50\u69cb\u60c5\u5831\u304c\u8ee2\u304c\u3063\u3066\u3044\u308b\u306e\u3067\u8a73\u7d30\u306f\u305d\u3061\u3089\u306b\u4efb\u305b\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Create_Datasets(Dataset):\n    def __init__(self, path, data_transform):\n        self.path = path\n        self.df = self.create_csv(path)\n        self.data_transform = data_transform\n\n    def create_csv(self, path):\n        image_path = &#91;]\n        for file_name in glob(path + '\/*.jpeg'):\n            basename = os.path.basename(file_name)\n            image_path.append(basename)\n\n        df = pd.DataFrame(image_path, columns=&#91;'path'])\n\n'''\n\u597d\u307f\u306e\u524d\u51e6\u7406\n'''\n\n        return df\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, i):\n        file = self.df&#91;'path']&#91;i]\n        score = np.array(self.df&#91;'good']&#91;i])\n        image = Image.open(os.path.join(self.path, file))\n        image = self.data_transform(image)\n\n        return image, score<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u500b\u4eba\u7684\u306b\u30cf\u30de\u3063\u305f\u843d\u3068\u3057\u7a74<\/h4>\n\n\n\n<p>\u4e0a\u8a18dataset\u3067\u5b66\u7fd2\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u305f\u969b\u306b\u3001loss\u306ebackward\u3067\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3002<br>\u30b3\u30fc\u30c9\u306f\u52d5\u304f\u304closs\u304c\u4e0b\u304c\u3089\u306a\u3044\u7b49\u304b\u306a\u308a\u6642\u9593\u3092\u53d6\u3089\u308c\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RuntimeError: Found dtype Double but expected Float<\/code><\/pre>\n\n\n\n<p>\u89e3\u6c7a\u7b56\u3068\u3057\u3066\u306flabel\u306edtype\u3092torch.float32\u306b\u3059\u308c\u3070\u826f\u3044\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>labels = labels.float().to(device)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7d42\u308f\u308a\u306b<\/h2>\n\n\n\n<p>\u4eca\u56de\u306ftorchvision\u306e\u30e2\u30c7\u30eb\u3092\u305d\u306e\u307e\u307e\u56de\u5e30\u306b\u6d41\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002<br>\u8efd\u304f\u52d5\u304b\u3057\u3066\u307f\u305f\u611f\u3058\u3067\u306f\u3001ResNet\u304c\u56de\u5e30\u3068\u306f\u76f8\u6027\u304c\u826f\u3055\u305d\u3046\u3067\u3059\u3002<br>\u5c11\u3057\u306e\u5909\u66f4\u3067\u6e08\u3080\u5272\u306b\u306f\u5168\u7136\u60c5\u5831\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b \u753b\u50cf\u306b\u3088\u308b\u56de\u5e30\u3001\u4f8b\u3048\u3070\u30e9\u30fc\u30e1\u30f3\u306e\u753b\u50cf\u304b\u3089\u98df\u3079\u30ed\u30b0\u8a55\u4fa1\u3092\u4e88\u6e2c\u3057\u305f\u308a\u3001\u4eba\u306e\u9854\u3092\u70b9\u6570\u5316\u3057\u305f\u308a\u306a\u3069\u69d8\u3005\u306a\u7528\u9014\u304c\u8003\u3048\u3089\u308c\u307e\u3059\u304c\u3001\u65e5\u672c\u8a9e\u3067\u3082\u82f1\u8a9e\u3067\u3082\u8cc7\u6599\u304c\u5c11\u306a\u304f\u3001\u4f55\u5ea6\u304b\u8e93\u3044\u305f\u306e\u3067\u5099\u5fd8\u9332\u3068\u3057\u3066\u307e\u3068\u3081\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u30e2\u30c7\u30eb\u306f\u2026<\/p>\n","protected":false},"author":1,"featured_media":339,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[7,29,30],"_links":{"self":[{"href":"https:\/\/is-ai.jp\/index.php?rest_route=\/wp\/v2\/posts\/337"}],"collection":[{"href":"https:\/\/is-ai.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/is-ai.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/is-ai.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/is-ai.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=337"}],"version-history":[{"count":2,"href":"https:\/\/is-ai.jp\/index.php?rest_route=\/wp\/v2\/posts\/337\/revisions"}],"predecessor-version":[{"id":380,"href":"https:\/\/is-ai.jp\/index.php?rest_route=\/wp\/v2\/posts\/337\/revisions\/380"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/is-ai.jp\/index.php?rest_route=\/wp\/v2\/media\/339"}],"wp:attachment":[{"href":"https:\/\/is-ai.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/is-ai.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/is-ai.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}