Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neomatrix369/91a7a42136e84ff606f85e3798c67eed to your computer and use it in GitHub Desktop.
Save neomatrix369/91a7a42136e84ff606f85e3798c67eed to your computer and use it in GitHub Desktop.
Update Pytorch model (weights) version from 4 to 3 (It a shell script you can use inside your Kaggle kernel (using the `%%bash` magic)
%%bash
apt-get install -y xarchiver || true
SAVED_MODEL_PATH=$(realpath "/kaggle/input/[/path/to/model/folder]")
NEW_MODEL_PATH=$(realpath "/kaggle/working")
for model_file in $(ls $SAVED_MODEL_PATH/*.pth)
do
just_filename=$(basename "${model_file%.*}")
version_filename_with_path="$(unzip -t $model_file | grep version | awk '{print $2}')"
if [[ ! -e "$NEW_MODEL_PATH/$just_filename.pth" ]]; then
echo "Copying $model_file to $NEW_MODEL_PATH"
cp $model_file /tmp
cd /tmp/
mkdir -p $(dirname $version_filename_with_path)
echo 3 > $version_filename_with_path
echo ""
zip -u $just_filename.pth $version_filename_with_path
rm -fr $(dirname $version_filename_with_path)
echo "Moving model file $just_filename.pth to $NEW_MODEL_PATH"
mv $just_filename.pth $NEW_MODEL_PATH/$just_filename.pth
fi
echo "(After) Contents of '$version_filename_with_path' in the model file '$model_file'"
unzip -p $NEW_MODEL_PATH/$(basename $model_file) $version_filename_with_path
done
echo "Your new model path is $NEW_MODEL_PATH, where you will find all the copied models (*.pth)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment