Submitting results to Kaggle competition from command line regardless of kernel type or file name, in or out of Kaggle - google-colaboratory

Within Kaggle: How can I submit my results to Kaggle competition regardless of kernel type or file name?
And if I am in a notebook outside Kaggle (Colab, Jupyter, Paperspace, etc.)?

Introduction (you can skip this part)
I was looking around for a method to do that. In particular, being able to submit at any point within the notebook (so you can test different approaches), a file with any name (to keep things separated), and any number of times (respecting the Kaggle limitations).
I found many webs explaining the process like
Making Submission
1. Hit the "Publish" button at the top of your notebook screen.
If you have written an output file, then you have an "Output" tab.
2. Output > Submit to Competition
However they fail to clarify that the Kernel must be of type "Script" and not "Notebook".
That has some limitations that I haven't fully explored.
I just wanted to be able to submit whatever file from the notebook, just like any other command within it.
The process
Well, here is the process I came up with.
Suggestions, errors, comments, improvements are welcome. Specifically I'd like to know why this method is no better than the one described above.
Process:
Install required libraries
Provide your kaggle credentials
using the file kaggle.json OR
setting some environment variables with your kaggle credentials
Submit with a simple command.
Q: Where do I get my kaggle credentials?
A: You get them from https://www.kaggle.com > 'Account' > "Create new API token"
1. Install required libraries
# Install required libraries
!pip install --upgrade pip
!pip install kaggle --upgrade
2. Provide your kaggle credentials -- setting some environment variables with your kaggle credentials
# Add your PRIVATE credentials
# Do not use "!export KAGGLE_USERNAME= ..." OR "" around your credential
%env KAGGLE_USERNAME=abc
%env KAGGLE_KEY=12341341
# Verify
!export -p | grep KAGGLE_USERNAME
!export -p | grep KAGGLE_KEY
See Note below.
2. Provide your kaggle credentials -- using the file kaggle.json
%mkdir --parents /root/.kaggle/
%cp /kaggle/input/<your_private_dataset>/kaggle.json /root/.kaggle/
!chmod 600 /root/.kaggle/kaggle.json
How you get the file there is up to you.
One simple way is this:
Download the kaggle.json to your computer
In kaggle, create a private dataset (Your_Profile > Datasets > New Dataset)
Add the kaggle.json to that Dataset
Add the private Dataset to your notebook ( Data > Add Data > Datasets > Your Datasets)
This may seem a bit cumbersome, but soon or later your API credentials may change and updating the file in one point (the dataset) will update it in all your notebooks.
(source: googleapis.com)
3. Submit with a simple command.
Here <competition-name> is the code name of the competition. You can get it from the url of the competition or from the section "My submissions" within the competition page.
(source: googleapis.com)
# Submit
!kaggle competitions submit -c <competition-name> -f submission.csv -m "Notes"
# example:
!kaggle competitions submit -c bike-sharing-demand -f submission.csv -m "Notes"
# View results
!kaggle competitions submissions -c <competition-name>
# example:
!kaggle competitions submissions -c bike-sharing-demand
Note:
If you are too conscious about security of your credentials and/or want to share the kernel, then you can type the 2 commands with your credentials on the "Console" instead of within the notebook (example below). They will be valid/available during that session only.
import os
os.environ['KAGGLE_USERNAME'] = "here DO use double quotes"
os.environ['KAGGLE_KEY'] = "here DO use double quotes"
You can find the console at the bottom of your kernel.
(source: googleapis.com)
PS: Initially this was posted here, but when the answer grew the Markdown display breaks in Kaggle (not in other places), therefore I had to take it out of Kaggle.

Related

gcloud CLI: running bq mk command requires a step using the browser

I am running the following command from a local terminal:
bq mk --transfer_config --target_dataset=mydataset --display_name='mytransfer' --params='{
"data_path": "s3://mys3path/*",
"destination_table_name_template": "mytable",
"file_format": "JSON",
"max_bad_records":"0",
"ignore_unknown_values":"true",
"access_key_id": "myaccessid",
"secret_access_key": "myaccesskey"
}' --data_source=amazon_s3
Now, every time I run this, I get the following:
/opt/google-cloud-sdk/platform/bq/bq.py:41: DeprecationWarning: the imp module is deprecated in favour of importlib and slated for removal in Python 3.12; see the module's documentation for alternative uses
import imp
Table '<mytablehere>' successfully created.
/opt/google-cloud-sdk/platform/bq/bq.py:41: DeprecationWarning: the imp module is deprecated in favour of importlib and slated for removal in Python 3.12; see the module's documentation for alternative uses
import imp
https://www.gstatic.com/bigquerydatatransfer/oauthz/auth?client_id=***********.apps.googleusercontent.com&scope=https://www.googleapis.com/auth/bigquery&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=version_info
Please copy and paste the above URL into your web browser and follow the instructions to retrieve a version_info.
Enter your version_info here:
So, every time I run this, I need to open this link, sign-in my account, authorize Google data transfer service to "View and manage your data in Google BigQuery and see the email address for your Google Account" and then copy/paste back to the terminal a string that I get in the browser.
Is there any way to persist the version configuration so that I don't have to perform this step every time?
Thank you in advance
In order to have your Service Account’s credentials to persist within the BigQuery command line tool, so that you can use it after logging out then logging in again, you will need to set the CLOUDSDK_PYTHON_SITEPACKAGES environment variable by running the following command:
export CLOUDSDK_PYTHON_SITEPACKAGES=1
You can then run the following command to see the accounts the tool has credentials for, which should include your Service Account:
gcloud auth list
I hope that the above information would be helpful. If it does not, make sure that you try out the steps followed in the Stackoverflow case.
Make sure to try out the .bigqueryrc solution provided by Michael Sheldon.

How can I find the status of an workflow execution?

For my current project, after I trigger a workflow, I need to check the status of its execution. I am not sure about the exact command. I have tried 'get-workflow' but it didn't seem to work.
There are a few ways, increasing in order of heavy-handedness.
You can hit with curl or something the API endpoint directly in Admin.
The Python SDK (flytekit) also ships with a command-line control plane utility called flyte-cli. In the future this may move to another location but it's here for now and you can hit it with this command.
flyte-cli -p yourproject -d development get-execution -u ex:yourproject:development:2fd90i
You can also use the Python class in flytekit that represents a workflow execution.
In [1]: from flytekit.configuration import set_flyte_config_file
In [2]: set_flyte_config_file('/Users/user/.flyte/config')
In [3]: from flytekit.common.workflow_execution import SdkWorkflowExecution
In [4]: e = SdkWorkflowExecution.fetch('yourproject', 'development', '2fd90i')

Jupyter: How to list running kernels and which notebook started them?

I haven't found answers in github or in the jupyter help.
TL;DR:
I need a command-line that tells me the same thing as the %connect_info magic, in particular the line Connect with --existing kernel-4e4c970e-cccb-2485-a09d-d56efd10e278.json.
My current setup is to have the jupyter notebook launched from a work server. Then I connect to that server with SSH, and can work in the same kernel by attaching a console to it:
jupyter console --existing kernel-4e4c970e-cccb-2485-a09d-d56efd10e278.json
(in a tmux+vim+slimux setup, which by the way I often find more convenient).
The problem is:
The only way to get the kernel string is from the notebook itself (which may be difficult to access over SSH);
I have several notebook runnings, sometimes I need to quickly check which one I need.
So is there a command to run from the shell on the server, that would list running kernels, which notebook launched them, and ideally if there are consoles attached?
You can list the kernel json files sorted by time with:
ls -tlr ~/.local/share/jupyter/runtime/
-rw------- 1 user user 263 6. Dez 13:40 kernel-c64635b5-6f5c-4938-828b-520d70e2a6c4.json
-rw------T 1 user user 263 7. Dez 11:57 kernel-1bdb41b4-1e87-4672-af92-7c3f3660e19c.json
-rw------T 1 user user 263 7. Dez 16:48 kernel-f7a51511-8c2a-4f0b-9cd3-6e5a79dccab3.json
This will not give you the information about which notebook launched them and if there is a console attached. But the times will give you some hints of which kernel is the one you are looking for, and with some trial and error you will figure it out.
This answer is based on the blog post “Connecting with Ipython from the command line to a running Jupyter session” by DS4N6.
A way to list to list running sessions it use the Jupyter REST API in conjunction with the jupyter lab list command (to obtain the token). This provides more information on the paths and files that are running - assuming you're running on localhost:8888:
curl -sSLG localhost:8888/api/sessions --data-urlencode `jupyter lab list | awk '/token/ {split($1,a,"?")} END {print a[2]}'` | jq

Submit a Keras training job to Google cloud

I am trying to follow this tutorial:
https://medium.com/#natu.neeraj/training-a-keras-model-on-google-cloud-ml-cb831341c196
to upload and train a Keras model on Google Cloud Platform, but I can't get it to work.
Right now I have downloaded the package from GitHub, and I have created a cloud environment with AI-Platform and a bucket for storage.
I am uploading the files (with the suggested folder structure) to my Cloud Storage bucket (basically to the root of my storage), and then trying the following command in the cloud terminal:
gcloud ai-platform jobs submit training JOB1
--module-name=trainer.cnn_with_keras
--package-path=./trainer
--job-dir=gs://mykerasstorage
--region=europe-north1
--config=gs://mykerasstorage/trainer/cloudml-gpu.yaml
But I get errors, first the cloudml-gpu.yaml file can't be found, it says "no such folder or file", and trying to just remove it, I get errors because it says the --init--.py file is missing, but it isn't, even if it is empty (which it was when I downloaded from the tutorial GitHub). I am Guessing I haven't uploaded it the right way.
Any suggestions of how I should do this? There is really no info on this in the tutorial itself.
I have read in another guide that it is possible to let gcloud package and upload the job directly, but I am not sure how to do this or where to write the commands, in my terminal with gcloud command? Or in the Cloud Shell in the browser? And how do I define the path where my python files are located?
Should mention that I am working with Mac, and pretty new to using Keras and Python.
I was able to follow the tutorial you mentioned successfully, with some modifications along the way.
I will mention all the steps although you made it halfway as you mentioned.
First of all create a Cloud Storage Bucket for the job:
gsutil mb -l europe-north1 gs://keras-cloud-tutorial
To answer your question on where you should write these commands, depends on where you want to store the files that you will download from GitHub. In the tutorial you posted, the writer is using his own computer to run the commands and that's why he initializes the gcloud command with gcloud init. However, you can submit the job from the Cloud Shell too, if you download the needed files there.
The only files we need from the repository are the trainer folder and the setup.py file. So, if we put them in a folder named keras-cloud-tutorial we will have this file structure:
keras-cloud-tutorial/
├── setup.py
└── trainer
├── __init__.py
├── cloudml-gpu.yaml
└── cnn_with_keras.py
Now, a possible reason for the ImportError: No module named eager error is that you might have changed the runtimeVersion inside the cloudml-gpu.yaml file. As we can read here, eager was introduced in Tensorflow 1.5. If you have specified an earlier version, it is expected to experience this error. So the structure of cloudml-gpu.yaml should be like this:
trainingInput:
scaleTier: CUSTOM
# standard_gpu provides 1 GPU. Change to complex_model_m_gpu for 4 GPUs
masterType: standard_gpu
runtimeVersion: "1.5"
Note: "standard_gpu" is a legacy machine type.
Also, the setup.py file should look like this:
from setuptools import setup, find_packages
setup(name='trainer',
version='0.1',
packages=find_packages(),
description='Example on how to run keras on gcloud ml-engine',
author='Username',
author_email='user#gmail.com',
install_requires=[
'keras==2.1.5',
'h5py'
],
zip_safe=False)
Attention: As you can see, I have specified that I want version 2.1.5 of keras. This is because if I don't do that, the latest version is used which has compatibility issues with versions of Tensorflow earlier than 2.0.
If everything is set, you can submit the job by running the following command inside the folder keras-cloud-tutorial:
gcloud ai-platform jobs submit training test_job --module-name=trainer.cnn_with_keras --package-path=./trainer --job-dir=gs://keras-cloud-tutorial --region=europe-west1 --config=trainer/cloudml-gpu.yaml
Note: I used gcloud ai-platform instead of gcloud ml-engine command although both will work. At some point in the future though, gcloud ml-engine will be deprecated.
Attention: Be careful when choosing the region in which the job will be submitted. Some regions do not support GPUs and will throw an error if chosen. For example, if in my command I set the region parameter to europe-north1 instead of europe-west1, I will receive the following error:
ERROR: (gcloud.ai-platform.jobs.submit.training) RESOURCE_EXHAUSTED:
Quota failure for project . The request for 1 K80
accelerators exceeds the allowed maximum of 0 K80, 0 P100, 0 P4, 0 T4,
0 TPU_V2, 0 TPU_V3, 0 V100. To read more about Cloud ML Engine quota,
see https://cloud.google.com/ml-engine/quotas.
- '#type': type.googleapis.com/google.rpc.QuotaFailure violations:
- description: The request for 1 K80 accelerators exceeds the allowed maximum of
0 K80, 0 P100, 0 P4, 0 T4, 0 TPU_V2, 0 TPU_V3, 0 V100.
subject:
You can read more about the features of each region here and here.
EDIT:
After the completion of the training job, there should be 3 folders in the bucket that you specified: logs/, model/ and packages/. The model is saved on the model/ folder a an .h5 file. Have in mind that if you set a specific folder for the destination you should include the '/' at the end. For example, you should set gs://my-bucket/output/ instead of gs://mybucket/output. If you do the latter you will end up with folders output, outputlogs and outputmodel. Inside output there should be packages. The job page link should direct to output folder so make sure to check the rest of the bucket too!
In addition, in the AI-Platform job page you should be able to see information regarding CPU, GPU and Network utilization:
Also, I would like to clarify something as I saw that you posted some related questions as an answer:
Your local environment, either it is your personal Mac or the Cloud Shell has nothing to do with the actual training job. You don't need to install any specific package or framework locally. You just need to have the Google Cloud SDK installed (in Cloud Shell is of course already installed) to run the appropriate gcloud and gsutil commands. You can read more on how exactly training jobs on the AI-Platform work here.
I hope that you will find my answer helpful.
I got it to work halfway now by not uploading the files but just running the upload commands from cloud at my local terminal... however there was an error during it running ending in "job failed"
Seems it was trying to import something from the TensorFlow backend called "from tensorflow.python.eager import context" but there was an ImportError: No module named eager
I have tried "pip install tf-nightly" which was suggested at another place, but it says I don't have permission or I am loosing the connection to cloud shell(exactly when I try to run the command).
I have also tried making a virtual environment locally to match that on gcloud (with Conda), and have made an environment with Conda with Python=3.5, Tensorflow=1.14.0 and Keras=2.2.5, which should be supported for gcloud.
The python program works fine in this environment locally, but I still get the (ImportError: No module named eager) when trying to run the job on gcloud.
I am putting the flag --python-version 3.5 when submitting the job, but when I write the command "Python -V" in the google cloud shell, it says Python=2.7. Could this be the issue? I have not fins a way to update the python version with the cloud shell prompt, but it says google cloud should support python 3.5. If this is anyway the issue, any suggestions on how to upgrade python version on google cloud?
It is also possible to manually there a new job in the google cloud web interface, doing this, I get a different error message: ERROR: Could not find a version that satisfies the requirement cnn_with_keras.py (from versions: none) and No matching distribution found for cnn_with_keras.py. Where cnn_with_keras.py is my python code from the tutorial, which runs fine locally.
Really don't know what to do next. Any suggestions or tips would be very helpful!
The issue with the GPU is solved now, it was something so simple as, my google cloud account had GPU settings disabled and needed to be upgraded.

after importing dill and dumping session , the notebook cannot be opened, the code cannot be retrieved

i worked on some experiment and wanted to find a way to save the lists of data i collected manually. i found dill and decided to use it .
installed via pip. typed in the following code:
import dill
dill.dump_session('notebook_env.db')
after that i closed the kernel and opened the notebook and to my shocking surprise got the following message:
Unreadable Notebook: /Users/nimrodros/Untitled.ipynb UnicodeDecodeError('utf-8', b'\x80\x03cdill._dill\n_import_module\nq\x00X\x08\x00\x00\x00__main__q\x01\x85q\x02Rq\x03}q\x04(X\x08\x00\x00\x00__name__q\x05h\x01X\x07\x00\x00\x00__doc__q\x06X#\x00\x00\x00Automatically created module for IPython interactive environmentq\x07X\x0b\x00\x00\x00__package__q\x08NX\x08\x00\x00\x00__spec__q\tNX\x0b\x00\x00\x00__builtin__q\nh\x00X\x08\x00\x00\x00builtinsq\x0b\x85q\x0cRq\rX\x03\x00\x00\x00_ihq\x0e]q\x0f(X\x00\x00\x00\x00q\x10X\x1a\x00\x00\x00eye_correction_middle_fallq\x11X\r\x00\x00\x00fuc=[1,2,3,4]q\x12X\x16\x00\x00\x00eye_correction_voltageq\x13X\r\x00\x00\x00fuc=[1,2,3,4]q\x14X/\x00\x00\x00import dill\ndill.dump_session(\'untitled.ipynb\')q\x15eX\x03\x00\x00\x00_ohq\x16}q\x17X\x03\x00\x00\x00_dhq\x18]q\x19X\x10\x00\x00\x00/Users/nimrodrosq\x1aaX\x02\x00\x00\x00Inq\x1bh\x0fX\x03\x00\x00\x00Outq\x1ch\x17X\x01\x00\x00\x00_q\x1dh\x10X\x02\x00\x00\x00__q\x1eh\x10X\x03\x00\x00\x00___q\x1fh\x10X\x02\x00\x00\x00_iq X\r\x00\x00\x00fuc=[1,2,3,4]q!X\x03\x00\x00\x00_iiq"X\x16\x00\x00\x00eye_correction_voltageq#X\x04\x00\x00\x00_iiiq$X\r\x00\x00\x00fuc=[1,2,3,4]q%%X\x03\x00\x00\x00_i1q&X\x1a\x00\x00\x00eye_correction_middle_fallq\'X\x03\x00\x00\x00_i2q(h%%X\x03\x00\x00\x00fucq)]q*(K\x01K\x02K\x03K\x04eX\x03\x00\x00\x00_i3q+h#X\x03\x00\x00\x00_i4q,h!X\x03\x00\x00\x00_i5q-X/\x00\x00\x00import dill\ndill.dump_session(\'untitled.ipynb\')q.X\x04\x00\x00\x00dillq/h\x00h/\x85q0Rq1ub.', 0, 1, 'invalid start byte')
why did this happen , and most importantly , how can i retrieve the code ? that data is already lost probably but i had valuable code there .
i believe if could open the file without running and just erase the line
import dill
dill.dump_session('notebook_env.db')
then i could again open the file without any problems . so maybe there's a way of doing that?
update
i tried to use
pip uninstall dill
hoped it would run the program again and stop when not recognizing dill , but it didn't go that way . still looking for a solution
update 2
tried to run the following on terminal :
iconv -f ISO-8859-1 -t UTF-8//TRANSLIT /Users/nimrodros/Untitled.ipynb -o /Users/nimrodros/New.ipynbh-MacBook-Pro-sl-nimrod:~ nimrodroTRANSLIT /Users/nimrodros/Untitled.ipynb -o /Users/nimrodros/New.ipynb
returned the following:
cdill._dill
_import_module
q__main__qqRq}q(__name__qhX__doc__qX#Automatically created module for IPython interactive environmentqX
__package__N__spec__q NX
__builtin__q
hbuiltinsq
q
fuc=[1,2,3,4]qX/import dillon_voltageqX
dill.dump_session('untitled.ipynb')qeX_ohq}qX_dhq]qX/Users/nimrodrofuc=[1,2,3,4]q%X_i1q&Xeye_correction_middle_fallq'X_i2q(h%Xfucq)]q*(KKKKeX_i3q+h#X_i4q,h!X_i5q-X/import dill
dill.dump_session('untitled.ipynb')q.Xdillq/hh/q0Rq1ub.iconv: -o: No such file or directory
iconv: /Users/nimrodros/New.ipynb: No such file or directory
h-MacBook-Pro-sl-nimrod:~ nimrodros$
the idea was to just convert it to a file that won't cause the notebook to crash so i could extract the valuable code. any ideas on how to continue?