BradLarson / AlexNet-Swift

An implementation of the standard AlexNet convolutional network in Swift for TensorFlow
37 stars 2 forks source link

not an issue - clarification on loading pretrained tensorflow weights #3

Closed johndpope closed 5 years ago

johndpope commented 5 years ago

In swift-models repo - there's a question about creating deep dream sample. https://github.com/tensorflow/swift-models/issues/157

Looking through another repo - there's some code here that uses inception 5h trained model. https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/14_DeepDream.ipynb

It's not apparently obvious in swift how to load a pre-trained (python tensorflow) model - but I'm thinking with your work here - maybe able to cherry pick the loading of weights.

ok - it seems this repo is re-doing all the training out of the box without leveraging any existing dataset.

https://www.mathworks.com/matlabcentral/fileexchange/59133-deep-learning-toolbox-model-for-alexnet-network The model has 23 layers and can classify images into 1000 object categories (e.g. keyboard, mouse, coffee mug, pencil).

I had imagined Alexnet was equivalent to MNIST - pre-trained data you could just load. Digging deeper - I found this - bvlc_alexnet.npy -- pretrained numpy weights for tensorflow http://www.cs.toronto.edu/~guerzhoy/tf_alexnet/

This won't magically load I guess due to architecture mismatch?

maybe that's an enhancement to this project for you to consider. If we can get a swift model sample loading trained weights - then that would help others.

BradLarson commented 5 years ago

For TensorFlow weights, you can use Raw.restoreV2() to pull weights from a checkpoint file. I believe an example of that can be found in here:

https://github.com/tensorflow/swift-models/blob/master/Transformer/PythonCheckpointReader.swift

for the weights here, I'd exported them from a trained Caffe model via a model loader that I've not included in the repository. I'm working on a more robust way to pull from .caffemodel files, as well as SavedModel and other protobuf-based files. Haven't had the time to complete that, though.

johndpope commented 5 years ago

nice thanks - a simple search for the method Raw.restoreV2() digs up a few repos. This one looks noteworthy for what appears to be simpler mapping. https://github.com/regrettable-username/style-transfer/blob/master/CheckpointReader.swift