How to download return from google collab? - python

I am using Google collab to analyze some data. I want to download the model file.
I have a code like that:
model.fit(x_train, y_train, epochs=1, batch_size=1,
I tried using the download(), but I doesn't work. Is there any way to download it?

I assuming that you are using Keras, because model.save() method return None so if you put it as download() method parameter, it will make an error. Just save model first then download it after that:
model.save('model_name.h5')
download('model_name.h5')
And make sure that you are at the correct directory that contain model_name.h5 file. You can check that with !ls command.

Related

Can't train model from checkpoint on Google Colab as session expires

I'm using Google Colab for finetuning a pre-trained model.
I successfully preprocessed a dataset and created an instance of the Seq2SeqTrainer class:
trainer = Seq2SeqTrainer(
model,
args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["validation"],
data_collator=data_collator,
tokenizer=tokenizer,
compute_metrics=compute_metrics
)
The problem is training it from last checkpoint after the session is over.
If I run trainer.train(), it runs correctly. As it takes a long time, I sometimes came back to the Colab tab after a few hours, and I know that if the session has crashed I can continue training from the last checkpoint like this: trainer.train("checkpoint-5500")
The checkpoint data does no longer exist on Google Colab if I come back too late, so even though I know the point the training has reached, I will have to start all over again.
Is there any way to solve this problem? i.e. extend the session?
To fix your problem try adding a full fixed path, for example for your google drive and saving the checkpoint-5500 to it.
Using your trainer you can set the output directory as your Google Drive path when creating an instance of the Seq2SeqTrainingArguments.
When you come back to your code, if the session is indeed over you'll just need to load your checkpoint-5500 from your google drive instead of retraining everything.
Add the following code:
from google.colab import drive
drive.mount('/content/drive')
And then after your trainer.train("checkpoint-5500") is finished (or as it's last step) save your checkpoint to your google drive.
Or if you prefer, you can add a callback inside your fit function in order to save and update after every single epoch (that was if for some reason the session is crashing before it finish you'll still have some progress saved).

How To Download Google Pegasus Library Model

I am a very newbie and currently working for my Final Project. I watch a youtube video that teach me to code Abstractive Text Summarization with google's Pegasus library. It Works fine but I need it to be more efficient.
So here is the code
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
tokenizer = PegasusTokenizer.from_pretrained("google/pegasus-xsum")
model = PegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum")
Everytime I run that code, it always download the "Google Pegasus-xsum" library which sized about 2.2 GB.
So here is the sample of the code in notebook : https://github.com/nicknochnack/PegasusSummarization/blob/main/Pegasus%20Tutorial.ipynb
and it will running download the library like picture below :
Is there any way to download the library first and then I saved it locally, and everytime I run the code it's just gonna call the library locally?
Something like caching or saving the library locally maybe?
Thanks.
Mac
Using inspect you can find and locate the modules easily.
import inspect
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
tokenizer = PegasusTokenizer.from_pretrained("google/pegasus-xsum")
model = PegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum")
print(inspect.getfile(PegasusForConditionalGeneration))
print(inspect.getfile(PegasusTokenizer))
You will get their paths sth like this
/usr/local/lib/python3.9/site-packages/transformers/models/pegasus/modeling_pegasus.py
/usr/local/lib/python3.9/site-packages/transformers/models/pegasus/tokenization_pegasus.py
Now, if you go and see what is inside the tokenization_pegasus.py file, you will notice that the model of google/pegasus-xsum is being probably fetched by the following line
PRETRAINED_VOCAB_FILES_MAP = {
"vocab_file": {"google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"}
}
where here if you open:
https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model
You will get the model downloaded directly to your machine.
UPDATE
After some search on Google, I've found sth important where you can get the used models and all their related files downloaded to your working directory by the following
tokenizer.save_pretrained("local_pegasus-xsum_tokenizer")
model.save_pretrained("local_pegasus-xsum_tokenizer_model")
Ref:
https://github.com/huggingface/transformers/issues/14561
So that after running it, you will see the following being saved automatically in your working directory. So, now you can call the models directly but you need to search how...
Also, the 12.2GB file that you wanted to know its path locally, it is being located here online
https://huggingface.co/google/pegasus-xsum/tree/main
And after downloading the models to your directory as you can see from the screenshot its name is pytorch_model.bin as it’s named online.

Deploying ML model on Streamlit

I have a code that basically takes in a csv, which can be uploaded from streamlit and then pushes out a classification prediction.
Just as a context I use xgboost to create my model and I save it as following:
joblib.dump(model, 'C:\\Users\myname\classification\default_class_model.pkl')
To grab the model I do:
model_from_joblib =joblib.load('C:\\Users\myname\classification\default_class_model.pkl')
scoring = model_from_joblib.predict(X_test)
When I execute it in Jupyter notebooks it seems to work just fine, but when running on anaconda and do
streamlit run mymodel.py
I get the error:
XGBoostError: [13:38:10]
C:\Users\Administrator\workspace\xgboost-win64_release_1.1.0\include\xgboost/json.h:65:
Invalid cast, from Null to Array
Does anyone have an idea why this may be?
I solved the problem by updating the xgboost version I was using

Can I pickle a tensorflow model?

Will I be able to pickle all the .meta, .data and checkpoint files of a tensorflow model? That's because I want to run a prediction on my model and if i deploy it , the files can't be on disk right? I know about the tensorflow serving but I don't really understand it. I want to be able to load the tensforflow files without accessing the drive all the time.
Using pickle is not recommended. Instead, they have created a new format called "SavedModel format" that serves this exact purpose.
See: https://www.tensorflow.org/guide/saved_model

Tensorboard : No dashboards active for current dataset

I am training a neural network for object detection using Google Colab. I wanted to visualize the learning process but every time I try to access tensorboard, it shows me the following:
No dashboards are active for the current data set. Probable causes: - You haven’t written any data to your event files. - TensorBoard can’t find your event files.
I am not training the model locally and have configured my google drive account with the colab notebook for the training data so user hpabst's answer does not seem useful.
I also tried setting up tensorboard using ngrok but that gave me a similar output.
I made sure I am generating summary data in a log directory by creating a summary writer:
import tensorflow as tf
sess = tf.Session()
file_writer = tf.summary.FileWriter('/content/logs/my_log_dir/', sess.graph)
and followed that with
tensorboard = TensorBoard(log_dir="/content/logs/my_log_dir/",batch_size=32, write_graph=True, update_freq='epoch')
model.fit_generator(
train_generator,
steps_per_epoch=(train_data/BS),
epochs=EPOCHS,
validation_data=validation_generator,
validation_steps=(test_data/BS),
callbacks=[tensorboard, checkpoint])
and finally
tensorboard --logdir /content/logs/my_log_dir/
The event files are in place. The path to the log directory is also correct.
Like I said, I was getting the same- No active dashboards error using ngrok. I moved over to the SCALARS menu in the Tensorboard GUI and to the left, under the runs section, at the bottom, I found out that the path to the log directory was being shown as '/content/ log /my_log_dir' although everywhere in my code I had only mentioned the path as -'/content/ logs /my_log_dir'. Maybe setting up tensorboard using ngrok expects the files to be in the 'log' and not the 'logs' directory. I made the change and now it works just fine.

Categories

Resources