{"id":311,"date":"2020-09-12T23:06:25","date_gmt":"2020-09-13T07:06:25","guid":{"rendered":"http:\/\/nramkumar.org\/tech\/?p=311"},"modified":"2020-09-20T20:18:03","modified_gmt":"2020-09-21T04:18:03","slug":"fixing-_shared_cuda_-unsupported-operation-and-out-of-memory-errors-with-fastai-lessons","status":"publish","type":"post","link":"https:\/\/nramkumar.org\/tech\/blog\/2020\/09\/12\/fixing-_shared_cuda_-unsupported-operation-and-out-of-memory-errors-with-fastai-lessons\/","title":{"rendered":"Fixing _share_cuda_ Unsupported Operation and Out of Memory Errors with fastai lessons"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">As mentioned <a href=\"https:\/\/nramkumar.org\/tech\/blog\/2020\/09\/12\/setting-up-and-running-jupyter-notebooks-for-the-fastai-book-locally-on-windows\/\" data-type=\"post\" data-id=\"309\">before<\/a>, I am trying to setup and run the fastai notebooks locally to get some hands-on exposure to deep learning. Unfortunately, if you are running this on Windows or have a very low-end GPU environment like myself you will run into additional problems. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, on Windows, I was hitting the error _<code>share_cuda_ operation not supported<\/code>. You can fix this error by adding <code>num_workers=0<\/code> parameter to the <code>ImageDataLoaders<\/code> call. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> The next error I hit was <code>CUDA out of memory<\/code> &#8211; You can fix this by adding the <code>bs=16<\/code> parameter (fine tune for your environment to optimize for speed without crashing &#8211; for me 64 hit OOM, 32 crashed the GPU and 16 balanced speed vs stability). The modified cell with all the fixes is below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#id first_training\n#caption Results from the first training\n# CLICK ME\nfrom fastai.vision.all import *\npath = untar_data(URLs.PETS)\/'images'\n\ndef is_cat(x): return x&#91;0].isupper()\ndls = ImageDataLoaders.from_name_func(\n    path, get_image_files(path), valid_pct=0.2, seed=42,\n    label_func=is_cat, item_tfms=Resize(224), num_workers=0, bs=16)\n\nlearn = cnn_learner(dls, resnet34, metrics=error_rate)\nlearn.fine_tune(1)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After this, I hit the <code>_share_cuda_ operation not supported<\/code> error in this block as well:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>img = PILImage.create(uploader.data&#91;0])\nis_cat,_,probs = learn.predict(img)\nprint(f\"Is this a cat?: {is_cat}.\")\nprint(f\"Probability it's a cat: {probs&#91;1].item():.6f}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unfortunately, learn.predict does not have a parameter that allows us to set <code>num_workers=0<\/code> which is required under Windows for me. I ended up having to edit learner.py in the fastai package and explicitly pass <code>n_workers=0<\/code> in the call to <code>get_preds<\/code>. This allowed me to complete the first cat\/dog classifier successfully!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As mentioned before, I am trying to setup and run the fastai notebooks locally to get some hands-on exposure to deep learning. Unfortunately, if you are running this on Windows or have a very low-end GPU environment like myself you will run into additional problems. First, on Windows, I was hitting the error _share_cuda_ operation&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,93],"tags":[],"class_list":["post-311","post","type-post","status-publish","format-standard","hentry","category-deep-learning","category-fastai"],"_links":{"self":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":2,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/311\/revisions\/313"}],"wp:attachment":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}