Making an unsupervised CNN model

Fahd_Jerbi: <p>Hi, I want to make a CNN model to work as a preprocessing model for images to do a task which is extracting linear features from images with CNN, here is an example below:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/c/cc1d70f4f9ff7f850f6a900e19bcf9df6e7e40ce.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cc1d70f4f9ff7f850f6a900e19bcf9df6e7e40ce" title="Image1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/c/cc1d70f4f9ff7f850f6a900e19bcf9df6e7e40ce_2_517x283.jpeg" alt="Image1" data-base62-sha1="t7GtR0QujsIGiyMC2TDaciwellk" width="517" height="283" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/c/cc1d70f4f9ff7f850f6a900e19bcf9df6e7e40ce_2_517x283.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/c/cc1d70f4f9ff7f850f6a900e19bcf9df6e7e40ce_2_775x424.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/c/cc1d70f4f9ff7f850f6a900e19bcf9df6e7e40ce_2_1034x566.jpeg 2x" data-dominant-color="EDEDED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Image1</span><span class="informations">1387×761 103 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I downloaded a pretrained model then I fine tuned it (changed weights and some layers), the model worked with train and test data (I visualized feature maps of each convolutional layer). My questions are:</p>
<ul>
<li>How to convert/use a supervised CNN model to an unsupervised CNN or at least how to pass  my image directly without splitting data ?</li>
<li>Is it possible to drop the fully connected layer in a CNN model because I saw an architecture like that in an article ?</li>
</ul>
<p>if anyone could help it would be appreciated.</p>

Dwight_Foster: <p>Ok I don’t think I can help you with the first problem but for the second one you should be able to just get the outputs of the cnn layers not the linear layers. It depends on the model though. What model were you thinking?</p>

Fahd_Jerbi: <p><a class="mention" href="/u/dwight_foster">@Dwight_Foster</a> actually I was thinking of Alexnet since it’s simple yet effective for my task I guess</p>

Dwight_Foster: <p>Ok to get just the convolutional layers for alexnet you could do something like this</p>
<pre><code class="lang-auto">model = torchvision.models.alexnet()
model.features
</code></pre>
<p>the model.features line will get only the convolutional layers and you can define that as a new model or just pass the input directly into it.</p>

Fahd_Jerbi: <p><a class="mention" href="/u/dwight_foster">@Dwight_Foster</a> thank you sir for your answer, just one more think is it possible to pass just one input without doing the train/test split method because I may need it for my task later ?</p>

Dwight_Foster: <p>yes you can pass just one input. You will still have to add the batch size dimension.</p>

Fahd_Jerbi: <p><a class="mention" href="/u/dwight_foster">@Dwight_Foster</a> could you sir provide me with an example if possible and thank you for your help</p>

Dwight_Foster: <p>ya no problem. Here you go:</p>
<pre><code class="lang-auto">model = torchvision.models.alexnet()
picture = #PICTURE YOU LOAD, must be in shape (batch_size, 3, size, size)
#you should have a batch size of one for one image. You can use .unsqueeze(0) to get  a batch dimension
output = model.features(picture)
</code></pre>