Model does not train when using Colab Premium GPU - gpu

I recently upgraded my google colab account to premium. But when I select the Premium GPU in runtime screenshot I encounter an issue while trying to train my model. The code runs and it does not show any error but it does not show any progress screenshot. Only when I change back to normal GPU under runtime the model trains.
Here is the code I am using to set up and train the model
model = ClassificationModel('roberta', 'roberta-base', num_labels=3, weight=class_weights.tolist(),
use_cuda=True, args={'reprocess_input_data': True, 'overwrite_output_dir': True,
"num_train_epochs": 10})
model.train_model(train)

Related

Losing variables while fixing runtime in Google Colab

I am training a machine learning model in google collab and fixed the automatically disconnecting by using this code in the console inspector view of this question (Google Colab session timeout):
function ClickConnect(){
console.log("Working");
document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click();
}
var clicker = setInterval(ClickConnect,60000);
However, after a given amount of time, all of my variables become undefined, which I require after the model has been trained. Is there any way to avoid this?

Any way to avoid the runtime restart when upgrading `google-cloud-language` in Google Colab?

I'm using Google's Natural Language API to run sentiment analysis on text blocks, and according to some instructions I'm following, I need t be using the latest version of google-cloud-language.
So I'm running this at the start of my Colab notebook.
!pip install --upgrade google-cloud-language
When I get to the end of that, it requires me to restart the runtime, which means I can't automatically run this along with my entire code, instead having to manually attend to the runtime restart.
This SO post touches on the topic, but only offers the 'crash' solution, and I'm wondering if anything else is available now 3 years later.
Restart kernel in Google Colab
So I'm curious if there's any workaround, or way to permanently upgrade google-cloud-language to avoid that?
Thank you for any input.
Here's the NL code I'm running, if helpful.
# Imports the Google Cloud client library
from google.cloud import language_v1
# Instantiates a client
client = language_v1.LanguageServiceClient()
def get_sentiment(text):
# The text to analyze
document = language_v1.Document(
content=text,
type_=language_v1.types.Document.Type.PLAIN_TEXT
)
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(
request={"document": document}
).document_sentiment
return sentiment
dfTW01["sentiment"] = dfTW01["text"].apply(get_sentiment)

How can I load a single image on Keras, using Google Colab?

I have built a deep learning model which classifies cats and dogs. I have successfully mounted Google drive and trained the images as needed. However, I am trying to make a single prediction by uploading one image and having Keras make a prediction.
In a regular IDE like Spyder, it's like this :
test_image = image.load_img('image1.jpg',target_size=(64,64))
But it throws this error :
Transport endpoint is not connected: 'image1.jpg'
I remounted the drive, and then it tells me :
No such file or directory: 'image1.jpg'
After that, I played with how I would write the directory on the image.load() method, but ran out of ideas at this point.
You can mount the drive, connect with an authentication, then import the files you want and predict using your model. Please check a GitHub gist here. You can follow the steps below and let me know if you need any help. Thanks!
from google.colab import drive
# This will prompt for authorization.
drive.mount('/content/drive')
# Predicting Roses
img=mpimg.imread('/content/drive/My Drive/5602220566_5cdde8fa6c_n.jpg')
imgplot = plt.imshow(img)
img = tf.expand_dims(img,0) # need this to make batch_shape = 1
img=img/255 # normalizing the image
img=tf.image.resize(img,size=(224, 224)) # resizing image
Prob=loaded_model.predict(img) # prediction
indd=tf.argmax(Prob[0],axis=-1).numpy()
print(tf.argmax(Prob[0],axis=-1).numpy())
print(labels_string[indd])

Session crash for an unknown reason when using pickle.dump in Google Colab

I am working on Google Colab environment to create a Siamese network using Keras. I have used this code from GitHub. But I get an error when I tried to run the pickle.dump code:
with open(os.path.join(save_path,"train.pickle"), "wb") as f:
pickle.dump((X,c), f)
The error : OverflowError: cannot serialize a bytes object larger than 4 GiB
So, I used Use pickle with protocol=4
pickle.dump((X,c), f, protocol=4)
but the session stopped during running this code and I got this message "Session crash for an unknown reason " and Your session crashed after using all available RAM
How can I solve this problem?
My guess is that your runtime is crashing out of memory.
I was able to pickle 4 GB of data, but it required ~8G of memory in Python to do so.
You can view the runtime logs with 'View runtime logs' for the Runtime menu. That often has hints about crashes. In this case, it reports many large allocations.
Example:
The sessions manager will show memory. In my case, without doing anything else:
I tried this and its working for me
import pickle
pickle_out = open("train.pickle","wb")
pickle.dump((X,c), pickle_out)
pickle_out.close()

Google word2vec load error

I want to use Google word2vec (GoogleNews-vectors-negative300.bin)
I downloaded it from https://code.google.com/archive/p/word2vec/
When I load it, the memory errors occured
(Process finished with exit code 139 (interrupted by signal 11: SIGSEGV))
from gensim.models.word2vec import Word2Vec
embedding_path = "data/GoogleNews-vectors-negative300.bin"
word2vec = Word2Vec.load_word2vec_format(embedding_path, binary=True)
print word2vec
I use ubuntu 16.04 / GTX-1070(8gb) / Ram(16gb).
How can I fix it?!
A SIGSEGV is an error occurring when the process tries to access a particular segment in memory that it does not have permission on.
So you should check permissions and, by debugging, see which memory location gives you the error.
This way you could understand if another program is interfering.
The problem might also be CUDA related as #TheM00s3 suggested