{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":39763,"databundleVersionId":11756775,"sourceType":"competition"}],"dockerImageVersionId":30919,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# OpenFWI Hands-on Tutorial for Kaggle Competition: Yale/UNC-CH - Geophysical Waveform Inversion\n![Status](https://img.shields.io/static/v1.svg?label=Status&message=finished&color=green)\n\n**GitHub OpenFWI code**: [![View files on Github](https://img.shields.io/static/v1.svg?logo=github&label=Repo&message=View%20On%20Github&color=lightgrey)](https://github.com/lanl/OpenFWI)\n\n**Tutorial Data**: [![Kaggle](https://img.shields.io/website?up_color=red&up_message=OpenFWI%20Website&url=https%3A%2F%2Fopenfwi-lanl.github.io%2F)](https://www.kaggle.com/competitions/waveform-inversion/data)  \n\n**Pre-trained Model**:[![GoogleDrive](https://img.shields.io/static/v1.svg?logo=google-drive&logoColor=yellow&label=GDrive&message=Download&color=yellow)](https://drive.google.com/drive/u/2/folders/1XlJNYsLDslrcle4YWzG4gIW5ogrK8qLE)   \n\n**OpenFWI Website**: [![Website](https://img.shields.io/website?up_color=red&up_message=OpenFWI%20Website&url=https%3A%2F%2Fopenfwi-lanl.github.io%2F)](https://openfwi-lanl.github.io)\n\n**Google Group**: [![Website](https://img.shields.io/website?up_color=red&up_message=Google%20Group&url=https%3A%2F%2Fgroups.google.com/g/openfwi%2F)](https://groups.google.com/g/openfwi)\n\n**Authors**: Hanchen Wang, Yinan Feng, Shihang Feng\n\n**Email**: hcwang@unc.edu, ynf@unc.edu, shihang.feng@live.com\n\n<br>\n\n\nThis tutorial provides a beginner-friendly introduction regarding the training and testing an InversionNet model with FlatVel-A Dataset.\n\nIn addition, we show how to visualize the seismic data and velocity maps.\n\nHere we use a small dataset sampled from the original FlatVel-A dataset, for the complete OpenFWI datasets, codes, and other information, refer to [our website](https://openfwi-lanl.github.io)","metadata":{}},{"cell_type":"markdown","source":"---\n## 1. Download the Codes\nDownload the code package on GitHub and go to the code directory.","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/lanl/OpenFWI\n%cd /kaggle/working/OpenFWI","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T04:51:40.629216Z","iopub.execute_input":"2025-05-06T04:51:40.629635Z","iopub.status.idle":"2025-05-06T04:51:40.752658Z","shell.execute_reply.started":"2025-05-06T04:51:40.629608Z","shell.execute_reply":"2025-05-06T04:51:40.751743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/working/OpenFWI\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T04:54:46.912815Z","iopub.execute_input":"2025-05-06T04:54:46.913185Z","iopub.status.idle":"2025-05-06T04:54:47.037340Z","shell.execute_reply.started":"2025-05-06T04:54:46.913160Z","shell.execute_reply":"2025-05-06T04:54:47.036345Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n## 2. Background: Seismic Survey\n\nAcoustic Wave Equation:\n $$\\nabla^2p(x,z,t)-\\frac{1}{c(x,z)^2}\\frac{\\partial ^2p(x,z,t)}{\\partial t ^2}=s\n$$\n\n<p align='center'> $s-$Seismic source $ \\ \\ \\ \\ $ $c-$Velocity Map $  \\ \\ \\ \\ $$p-$Seismic wavefield</p>\n\n<br>\n\nSeismic source $s$, such as thumper truck, explosive, air guns and so on, generates the seismic wave, which travel through the subsurface medium such as layer of rocks or water. In this tutorial, we assume the source function is known.\n\nVelocity map $c$ describes the speed with which the wave propagates through the medium.\n\nSeismic wavefield $p$ provides the information of seismic wave propagtion in the subsurface.\n\n<br>\nWe need to know the seismic wavefield $p$ to invert for velocity map $c$. But it is very expensive to put the sensors beneath the ground surface. Instead  we put the geophones on the surface to record the wave motion on the surface as seismic data $p(x=g,z=0,t)$, where $g$ is the location of geophones on the surface.\n\n<p align='center'>Inversion problem: $p(g,t)$→$c(x,z)$</p>\n\n\n\n<img src=\"https://media.licdn.com/dms/image/v2/D4D12AQGwePRv5IyNbg/article-cover_image-shrink_720_1280/article-cover_image-shrink_720_1280/0/1705583268851?e=1749686400&v=beta&t=aYDIgFYnipDIZ1__3YrO7FjPoxVqvc6fbacR_NYvS-Q\" width=\"800\">\n\n*An example of seismic exploration. Image source: LinkedIn: https://www.linkedin.com/pulse/seismic-survey-planning-pmp-framework-himanshu-bhardwaj-mzdaf/*","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/input/waveform-inversion/train_samples/FlatVel_A/data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T04:40:05.427312Z","iopub.execute_input":"2025-05-06T04:40:05.427607Z","iopub.status.idle":"2025-05-06T04:40:05.550819Z","shell.execute_reply.started":"2025-05-06T04:40:05.427584Z","shell.execute_reply":"2025-05-06T04:40:05.549696Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n## 3. Download the Tutorial Data\nPlease add this Kaggle competition (Yale/UNC-CH - Geophysical Waveform Inversion) dataset to your current directory: \n\n**1) On the right hand side panel, click on \"Add Input\";**\n\n**2) Search keyword: waveform-inversion;**\n\n**3) Click on the \"Yale/UNC-CH - Geophysical Waveform Inversion\" competition.**\n\nYou may follow this link to add Kaggle competition dataset to your notebook: https://medium.com/getting-started-in-data-science/how-to-add-data-to-your-kaggle-notebook-4084b4cfa45f\n<br>\n<br>\n<p align='left'>Data: Seismic data $p(g,t)$ $\\ \\ \\ \\ $ Label: Velocity Map $c(x,z)$</p>\n<br>\nThere are two pairs of data and label:\n\n*model1.npy* and *data1.npy*\n\n*model2.npy* and *data2.npy*\n","metadata":{}},{"cell_type":"markdown","source":"---\n## 4. Data and Label Visualization\n\nLoad data and label for visualization\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nvelocity = np.load('/kaggle/input/waveform-inversion/train_samples/FlatVel_A/model/model2.npy')\ndata = np.load('/kaggle/input/waveform-inversion/train_samples/FlatVel_A/data/data2.npy')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T03:50:46.101283Z","iopub.execute_input":"2025-05-06T03:50:46.101467Z","iopub.status.idle":"2025-05-06T03:50:50.439423Z","shell.execute_reply.started":"2025-05-06T03:50:46.101449Z","shell.execute_reply":"2025-05-06T03:50:50.438532Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Check the size of the velocity map and seismic data\n","metadata":{}},{"cell_type":"code","source":"print('Velocity map size:', velocity.shape)\nprint('Seismic data size:', data.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T03:59:45.420366Z","iopub.execute_input":"2025-05-06T03:59:45.420690Z","iopub.status.idle":"2025-05-06T03:59:45.425655Z","shell.execute_reply.started":"2025-05-06T03:59:45.420665Z","shell.execute_reply":"2025-05-06T03:59:45.424927Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Velocity Map Vistualization\n\nEach file contains 120 samples.\n\nVelocity Map $c(x,z)$:\n\n$nx=70$, $nz=70$\n\n$dx=10\\ m$, $dz=10\\ m$","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n## Select a sample in the data\nsample=14","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T03:59:54.318779Z","iopub.execute_input":"2025-05-06T03:59:54.319123Z","iopub.status.idle":"2025-05-06T03:59:54.323104Z","shell.execute_reply.started":"2025-05-06T03:59:54.319095Z","shell.execute_reply":"2025-05-06T03:59:54.322247Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"\n\nfrom matplotlib.colors import ListedColormap\n\nfig, ax = plt.subplots(1, 1, figsize=(11, 5))\nimg=ax.imshow(velocity[sample,0,:,:],cmap='jet')\nax.set_xticks(range(0, 70, 10))\nax.set_xticklabels(range(0, 700, 100))\nax.set_yticks(range(0, 70, 10))\nax.set_yticklabels(range(0, 700, 100))\nax.set_ylabel('Depth (m)', fontsize=12)\nax.set_xlabel('Offset (m)', fontsize=12)\nclb=plt.colorbar(img, ax=ax)\nclb.ax.set_title('km/s',fontsize=8)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T04:00:15.521773Z","iopub.execute_input":"2025-05-06T04:00:15.522096Z","iopub.status.idle":"2025-05-06T04:00:15.929638Z","shell.execute_reply.started":"2025-05-06T04:00:15.522039Z","shell.execute_reply":"2025-05-06T04:00:15.928660Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"In the real world, the flat rock layers look like this:\n\n<left width=\"100%\" style=\"padding:10px\"><img src=\"http://2.bp.blogspot.com/-S0ztekHkBEY/Vh7DzDlMgAI/AAAAAAAACM4/AQ6BU8pcxnU/s1600/rock-layers-3578_640.jpg\" width=\"400px\"></left>\n\n*An example of rock layers. Image source: http://geologylearn.blogspot.com/*\n","metadata":{}},{"cell_type":"markdown","source":"### Wave Propagation in the Velocity Map\n\nWe put surface sources at 5 different location and generate 5 different seismic data.\n\nLet see an example about how the waves progagate in the velocity map.\n","metadata":{}},{"cell_type":"code","source":"! wget --no-check-certificate 'https://zenodo.org/record/7293942/files/tutorial_wave.mp4?download=1'  -O /kaggle/working/wave_propagation.mp4\n\nfrom IPython.display import HTML\nfrom base64 import b64encode\nmp4 = open('/kaggle/working/wave_propagation.mp4','rb').read()\ndata_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\nHTML(\"\"\"\n<video width=1200 controls>\n      <source src=\"%s\" type=\"video/mp4\">\n</video>\n\"\"\" % data_url)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T04:02:24.418990Z","iopub.execute_input":"2025-05-06T04:02:24.419387Z","iopub.status.idle":"2025-05-06T04:02:24.972999Z","shell.execute_reply.started":"2025-05-06T04:02:24.419349Z","shell.execute_reply":"2025-05-06T04:02:24.971864Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Direct Wave Traveltime:\n\n $$t_{direct}=\\frac{d}{c}$$\n\nReflection Wave Traveltime:\n $$t_{refl}=\\frac{\\sqrt{d^2+4h^2}}{c}$$\n\n<center width=\"100%\" style=\"padding:10px\"><img src=\"https://openfwi-lanl.github.io/assets/img/tutorial_path.png\" width=\"400px\"></center>","metadata":{}},{"cell_type":"markdown","source":"### Seismic Data Vistualization\n\nSeismic Data $p(g,t)$:\n\n$nt=1000$, $ng=70$\n\n$dt=0.001\\ s$, $dg=10\\ m$","metadata":{}},{"cell_type":"code","source":"print('Seismic data size:', data.shape)\n\nfig,ax=plt.subplots(1,5,figsize=(20,5))\nax[0].imshow(data[sample,0,:,:],extent=[0,70,1000,0],aspect='auto',cmap='gray',vmin=-0.5,vmax=0.5)\nax[1].imshow(data[sample,1,:,:],extent=[0,70,1000,0],aspect='auto',cmap='gray',vmin=-0.5,vmax=0.5)\nax[2].imshow(data[sample,2,:,:],extent=[0,70,1000,0],aspect='auto',cmap='gray',vmin=-0.5,vmax=0.5)\nax[3].imshow(data[sample,3,:,:],extent=[0,70,1000,0],aspect='auto',cmap='gray',vmin=-0.5,vmax=0.5)\nax[4].imshow(data[sample,4,:,:],extent=[0,70,1000,0],aspect='auto',cmap='gray',vmin=-0.5,vmax=0.5)\nfor axis in ax:\n   axis.set_xticks(range(0, 70, 10))\n   axis.set_xticklabels(range(0, 700, 100))\n   axis.set_yticks(range(0, 2000, 1000))\n   axis.set_yticklabels(range(0, 2,1))\n   axis.set_ylabel('Time (s)', fontsize=12)\n   axis.set_xlabel('Offset (m)', fontsize=12)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T04:12:04.947126Z","iopub.execute_input":"2025-05-06T04:12:04.947473Z","iopub.status.idle":"2025-05-06T04:12:05.511515Z","shell.execute_reply.started":"2025-05-06T04:12:04.947448Z","shell.execute_reply":"2025-05-06T04:12:05.510652Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n## 5. Inversion Network\n<p align='center'>Inversion problem: $p(g,t)$→$c(x,z)$</p>\n\nIn this tutorial, we use InversionNet ([Wu and Lin, 2019](https://ieeexplore.ieee.org/abstract/document/8918045)):\n\nThe network structure (see *network.py*)\n\n<center width=\"100%\" style=\"padding:10px\"><img src=\"https://openfwi-lanl.github.io/assets/img/InversionNet.png\" width=\"1600px\"></center>\n\n\nThe network is mainly composed of 2D convolution layers and transposed 2D convolution layers.\n\n<left width=\"100%\" style=\"padding:10px\"><img src=\"https://editor.analyticsvidhya.com/uploads/33383str.jpg\" width=\"600px\"></left>\n\n*2D convolution with no padding, stride of 2 and kernel of 3. Image source: https://www.analyticsvidhya.com/blog/2022/03/basics-of-cnn-in-deep-learning/\n\n\n<left width=\"100%\" style=\"padding:10px\"><img src=\"https://miro.medium.com/max/790/1*Lpn4nag_KRMfGkx1k6bV-g.gif\" width=\"300px\"></left>\n\n*Transposed 2D convolution with no padding, stride of 2 and kernel of 3.Image source: https://towardsdatascience.com/types-of-convolutions-in-deep-learning-717013397f4d\n","metadata":{}},{"cell_type":"markdown","source":"### Training\n**Main file**: *train.py*\n\n**Dataset related**:\n\n* \"*-ds flat-tutorial*\"$-$ Dataset name, such as shape, spacing, maximum and minimum value (listed in dataset_config.json).\n\n**Path related**:\n\n* \"*-n tutorial*\"$-$ Folder name for this experiment\n\n* \"*-t tutorial_train.txt*\"$-$ The directory of training set. *fva_velocity1.npy*, *fva_data1.npy*, *fva_velocity2.npy* and *fva_data2.npy* are the training set.\n\n\n* \"*-v tutorial_val.txt*\"$-$ The directory of validation set. *fva_velocity3.npy* and *fva_data3.npy* are the validation set.\n\n**Model related**:\n\n* \"*-m InversionNet*\"$-$ Network name (refers to *network.py*).\n\n\n**Training related**:\n\n* *-g1v 1*$-$ $\\ell_1$ loss function weight\n\n* *-g2v 0*$-$ $\\ell_2$ loss function weight\n\n\n* \"*--lr 0.0001*\"$-$ Learning rate\n\n* \"*-b 120*\"$-$ Batch size\n\n* \"*-eb 10*\"$-$ Epochs in a saved model. Save the best model every 10 epochs.\n\n* \"*-nb 5*\"$-$ Number of saved model. Save 4 models means total epochs number is $10\\times5=50$\n\n**Tensorboard related**:\n\n* \"*--tensorboard*\"$-$ Use tensorboard for logging.\n\n\n(More details about input parameters in *train.py*)","metadata":{}},{"cell_type":"code","source":"!python train.py -ds flatvel-tutorial -n tutorial -m InversionNet -g1v 1 -g2v 0  --tensorboard -t kaggle_tutorial_train.txt -v kaggle_tutorial_val.txt  --lr 0.0001 -b 120 -eb 10 -nb 5","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T04:40:47.127493Z","iopub.execute_input":"2025-05-06T04:40:47.127846Z","iopub.status.idle":"2025-05-06T04:44:23.288977Z","shell.execute_reply.started":"2025-05-06T04:40:47.127817Z","shell.execute_reply":"2025-05-06T04:44:23.288133Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%pwd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T05:16:25.165151Z","iopub.execute_input":"2025-05-06T05:16:25.165478Z","iopub.status.idle":"2025-05-06T05:16:25.171789Z","shell.execute_reply.started":"2025-05-06T05:16:25.165454Z","shell.execute_reply":"2025-05-06T05:16:25.171134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/working/OpenFWI","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T05:26:38.460904Z","iopub.execute_input":"2025-05-06T05:26:38.461291Z","iopub.status.idle":"2025-05-06T05:26:38.582009Z","shell.execute_reply.started":"2025-05-06T05:26:38.461262Z","shell.execute_reply":"2025-05-06T05:26:38.581262Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Testing\n\n**Main file**: *test.py*\n\n**Dataset related**:\n\n* \"*-ds flat-tutorial*\"$-$ Dataset Name, such as shape, spacing, maximum and minimum value (listed in dataset_config.json).\n\n**Path related**:\n\n* \"*-n tutorial*\"$-$ Folder name for this experiment\n\n* \"*-t tutorial_train.txt*\"$-$ The directory of dataset you want to test.\n\n**Model related**:\n\n* \"*-m InversionNet*\"$-$ Network name (refers to *network.py*).\n\n* \"*-r checkpoint.pth\"$-$ Load saved network parameters (default is checkpoint.pth).\n\n**Visualization related**:\n\n* \"*--vis*\"$-$ Visualization option.\n\n* \"*-vb 2*\"$-$ Number of batch to be visualized.\n\n* \"*-vsa 3*\"$-$ Number of samples in a batch to be visualized.\n\n(More details about input parameters in *test.py*)\n<br>\n### Test on Training Set\n\n\nCheck how the trained network perfom on the training data, we set \"-v tutorial_train.txt\"\n\nTo test the network's performance on training set *fva_velocity1.npy*, *fva_data1.npy*, *fva_velocity2.npy* and *fva_data2.npy*\n\n","metadata":{}},{"cell_type":"code","source":"! ls /kaggle/working/OpenFWI","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T05:46:53.300616Z","iopub.execute_input":"2025-05-06T05:46:53.300920Z","iopub.status.idle":"2025-05-06T05:46:53.421193Z","shell.execute_reply.started":"2025-05-06T05:46:53.300898Z","shell.execute_reply":"2025-05-06T05:46:53.420177Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! ls /kaggle/working/OpenFWI/split_files","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T05:45:31.935631Z","iopub.execute_input":"2025-05-06T05:45:31.935973Z","iopub.status.idle":"2025-05-06T05:45:32.056564Z","shell.execute_reply.started":"2025-05-06T05:45:31.935947Z","shell.execute_reply":"2025-05-06T05:45:32.055550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define the file path\nfile_path = \"/kaggle/working/OpenFWI/split_files/kaggle_tutorial_val.txt\"\n\n# Open the file in read mode and store its contents\nwith open(file_path, 'r') as file:\n    content = file.read()\n\n# Print the file's contents\nprint(content)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T05:59:27.757417Z","iopub.execute_input":"2025-05-06T05:59:27.757780Z","iopub.status.idle":"2025-05-06T05:59:27.763094Z","shell.execute_reply.started":"2025-05-06T05:59:27.757755Z","shell.execute_reply":"2025-05-06T05:59:27.762292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! ls /kaggle/working/OpenFWI/Invnet_models/tutorial","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T05:44:25.143151Z","iopub.execute_input":"2025-05-06T05:44:25.143492Z","iopub.status.idle":"2025-05-06T05:44:25.263800Z","shell.execute_reply.started":"2025-05-06T05:44:25.143464Z","shell.execute_reply":"2025-05-06T05:44:25.262804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python test.py -ds flatvel-tutorial -n tutorial -m InversionNet -v kaggle_tutorial_train.txt -r checkpoint.pth --vis -vb 2 -vsa 3 -o /kaggle/working/OpenFWI/Invnet_models/\n\nfrom IPython.display import Image\nfrom IPython.display import display\na=Image('./Invnet_models/tutorial/visualization/V_0_0.png', width = 600, height = 300)\nb=Image('./Invnet_models/tutorial/visualization/V_0_1.png', width = 600, height = 300)\nc=Image('./Invnet_models/tutorial/visualization/V_0_2.png', width = 600, height = 300)\ndisplay(a,b,c)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T05:59:52.026116Z","iopub.execute_input":"2025-05-06T05:59:52.026424Z","iopub.status.idle":"2025-05-06T06:00:04.996890Z","shell.execute_reply.started":"2025-05-06T05:59:52.026399Z","shell.execute_reply":"2025-05-06T06:00:04.996089Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Loss\n\n$\\ell_1$ loss: $\\ MAE(x,y)=\\frac{\\sum_{i=1}^n|y_i-x_i|}{n}$\n\n\n$\\ell_2$ loss:   $\\ MSE(x,y)=\\frac{\\sum_{i=1}^n(y_i-x_i)^2}{n}$\n\n\n\n\nStructural Similarity Index Measure (SSIM): $\\ SSIM(x,y)=\\frac{(2\\mu_x\\mu_y+c_1)(2\\delta_{xy}+c_2)}{(\\mu_x^2+\\mu_y^2+c_1)(\\delta_x^2+\\delta_y^2+c_2)}$\n\n$\\mu_x-$ the pixel sample mean of x\n\n$\\mu_y-$ the pixel sample mean of y\n\n$\\delta_x^2-$ the variance of x\n\n$\\delta_y^2-$ the variance of y\n\n$\\delta_{xy}-$ the covariance of x and y\n\n$c_1,c_2-$ two variables to stabilize the division with weak denominator","metadata":{}},{"cell_type":"markdown","source":"### Test on Validation Set\nCheck how the trained network perfom on the validation data, we set \"-v tutorial_val.txt\"\n\nTo test the network's performance on validation set *fva_velocity3.npy* and *fva_data3.npy*.","metadata":{}},{"cell_type":"code","source":"!python test.py -ds flatvel-tutorial -n tutorial -m InversionNet -v kaggle_tutorial_val.txt -r checkpoint.pth --vis -vb 2 -vsa 3 -o /kaggle/working/OpenFWI/Invnet_models/\n\na=Image('./Invnet_models/tutorial/visualization/V_0_0.png', width = 600, height = 300)\nb=Image('./Invnet_models/tutorial/visualization/V_0_1.png', width = 600, height = 300)\nc=Image('./Invnet_models/tutorial/visualization/V_0_2.png', width = 600, height = 300)\ndisplay(a,b,c)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T05:55:02.712584Z","iopub.execute_input":"2025-05-06T05:55:02.712902Z","iopub.status.idle":"2025-05-06T05:55:15.828740Z","shell.execute_reply.started":"2025-05-06T05:55:02.712880Z","shell.execute_reply":"2025-05-06T05:55:15.827833Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n### Download Pre-trained Model\nThe pre-trained models are available at [![GoogleDrive](https://img.shields.io/static/v1.svg?logo=google-drive&logoColor=yellow&label=GDrive&message=Download&color=yellow)](https://drive.google.com/drive/u/2/folders/1XlJNYsLDslrcle4YWzG4gIW5ogrK8qLE). Here we download *fva_l1.pth*\n\n*fva_l1.pth* is trained with full FlatVel_A dataset 24,000 training samples and 6,000 validataion samples. (**Full Data**:![Website](https://img.shields.io/website?up_color=red&up_message=OpenFWI%20dataset&url=https%3A%2F%2Fopenfwi-lanl.github.io%2Fdocs%2Fdata.html))\n\nHere we download *fva_l1.pth*, which is the InversionNet model trained on FlatVel-A dataset with *l1* loss.\n\n\n","metadata":{}},{"cell_type":"code","source":"! mkdir -p /kaggle/working/Invnet_models/pretrained_model/\n! wget --no-check-certificate 'https://zenodo.org/record/7293942/files/fva_l1.pth?download=1'   -O /kaggle/working/Invnet_models/pretrained_model/fva_l1.pth","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T23:41:57.533625Z","iopub.execute_input":"2025-04-07T23:41:57.533998Z","iopub.status.idle":"2025-04-07T23:42:24.938354Z","shell.execute_reply.started":"2025-04-07T23:41:57.53397Z","shell.execute_reply":"2025-04-07T23:42:24.937318Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Test with Pre-trained Model\n\n* \"*-r fva_l1.pth\"$-$ Load pre-trained Model fva_l1.pth\n","metadata":{}},{"cell_type":"code","source":"!python test.py -ds flatvel-tutorial -n pretrained_model -m InversionNet -v kaggle_tutorial_val.txt -r fva_l1.pth --vis -vb 2 -vsa 3 -o /kaggle/working/Invnet_models/\na=Image('/kaggle/working/Invnet_models/pretrained_model/visualization/V_0_0.png', width = 600, height = 300)\nb=Image('/kaggle/working/Invnet_models/pretrained_model/visualization/V_0_1.png', width = 600, height = 300)\nc=Image('/kaggle/working/Invnet_models/pretrained_model/visualization/V_0_2.png', width = 600, height = 300)\ndisplay(a,b,c)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T23:42:27.693026Z","iopub.execute_input":"2025-04-07T23:42:27.693361Z","iopub.status.idle":"2025-04-07T23:42:40.607748Z","shell.execute_reply.started":"2025-04-07T23:42:27.693332Z","shell.execute_reply":"2025-04-07T23:42:40.60689Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Transfer Learning\nThe pre-trained models are available at [![GoogleDrive](https://img.shields.io/static/v1.svg?logo=google-drive&logoColor=yellow&label=GDrive&message=Download&color=yellow)](https://drive.google.com/drive/u/2/folders/1XlJNYsLDslrcle4YWzG4gIW5ogrK8qLE). Here we download *fva_l1.pth*\n\n*cva_l1.pth* is trained with full CurveVel_A dataset 24,000 training samples and 6,000 validataion samples.\n\nAn example of CurveVel_A velocity map:\n\n<left width=\"100%\" style=\"padding:10px\"><img src=\"https://openfwi-lanl.github.io/assets/img/tutorial_curve.png\" width=\"200px\"></left>\n\nIn the real world, the curved rock layers look like this:\n\n<left width=\"100%\" style=\"padding:10px\"><img src=\"https://i0.wp.com/canyonministries.com.s3.amazonaws.com/wp-content/uploads/2015/12/Israel-Fold.jpg\" width=\"400px\"></left>\n\n*An example of bent rock layers. Image source: https://www.canyonministries.org/bent-rock-layers/\n\n","metadata":{}},{"cell_type":"code","source":"!mkdir -p /kaggle/working/Invnet_models/transfer_learning/\n! wget --no-check-certificate 'https://zenodo.org/record/7293942/files/cva_l1.pth?download=1'   -O /kaggle/working/Invnet_models/transfer_learning/cva_l1.pth","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T23:42:45.764322Z","iopub.execute_input":"2025-04-07T23:42:45.764645Z","iopub.status.idle":"2025-04-07T23:43:34.338942Z","shell.execute_reply.started":"2025-04-07T23:42:45.764617Z","shell.execute_reply":"2025-04-07T23:43:34.337918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python test.py -ds flatvel-tutorial -n transfer_learning -m InversionNet -v kaggle_tutorial_val.txt -r cva_l1.pth --vis -vb 2 -vsa 3 -o /kaggle/working/Invnet_models/\n\nfrom IPython.display import Image\nfrom IPython.display import display\na=Image('/kaggle/working/Invnet_models/transfer_learning/visualization/V_0_0.png', width = 600, height = 300)\nb=Image('/kaggle/working/Invnet_models/transfer_learning/visualization/V_0_1.png', width = 600, height = 300)\nc=Image('/kaggle/working/Invnet_models/transfer_learning/visualization/V_0_2.png', width = 600, height = 300)\ndisplay(a,b,c)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T23:43:37.765277Z","iopub.execute_input":"2025-04-07T23:43:37.76559Z","iopub.status.idle":"2025-04-07T23:43:50.750582Z","shell.execute_reply.started":"2025-04-07T23:43:37.765565Z","shell.execute_reply":"2025-04-07T23:43:50.749632Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Resume training with pre-trained Model cva_l1.pth\n\n* \"*-r cva_l1.pth\"$-$ Resume from pre-trained Model cva_l1.pth\n\n* \"*-eb 25*\"$-$ Epochs in a saved model. Save the best model every 25 epochs.\n\n* \"*-nb 7*\"$-$ Number of saved model. Save 4 models means total epochs number is $25\\times6=150$\n\ncva_l1.pth is already trained for 119 epochs, then we need to train additional 31 epochs\n!python train.py -ds flatvel-tutorial -n transfer_learning -m InversionNet -g1v 1 -g2v 0  --tensorboard -t tutorial_train.txt -v tutorial_val.txt\\\n  --lr 0.0001 -b 120 -eb 25 -nb 6 -r cva_l1.pth","metadata":{}},{"cell_type":"code","source":"!python train.py -ds flatvel-tutorial -n transfer_learning -m InversionNet -g1v 1 -g2v 0  --tensorboard -t kaggle_tutorial_train.txt -v kaggle_tutorial_val.txt\\\n  --lr 0.0001 -b 120 -eb 25 -nb 5 -r cva_l1.pth -o /kaggle/working/Invnet_models/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T23:43:56.068341Z","iopub.execute_input":"2025-04-07T23:43:56.068644Z","iopub.status.idle":"2025-04-07T23:44:53.221018Z","shell.execute_reply.started":"2025-04-07T23:43:56.06862Z","shell.execute_reply":"2025-04-07T23:44:53.219972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python test.py -ds flatvel-tutorial -n transfer_learning -m InversionNet -v kaggle_tutorial_val.txt -r checkpoint.pth --vis -vb 2 -vsa 3 -o /kaggle/working/Invnet_models/\na=Image('/kaggle/working/Invnet_models/transfer_learning/visualization/V_0_0.png', width = 600, height = 300)\nb=Image('/kaggle/working/Invnet_models/transfer_learning/visualization/V_0_1.png', width = 600, height = 300)\nc=Image('/kaggle/working/Invnet_models/transfer_learning/visualization/V_0_2.png', width = 600, height = 300)\ndisplay(a,b,c)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T23:44:56.235413Z","iopub.execute_input":"2025-04-07T23:44:56.235763Z","iopub.status.idle":"2025-04-07T23:45:09.333442Z","shell.execute_reply.started":"2025-04-07T23:44:56.235737Z","shell.execute_reply":"2025-04-07T23:45:09.332553Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Test with your own machine\nDue to the limitation of Colab, we only provide small dataset for the tutorial. The full dataset can be reached through our website:\n[![Website](https://img.shields.io/website?up_color=red&up_message=OpenFWI%20dataset&url=https%3A%2F%2Fopenfwi-lanl.github.io%2Fdocs%2Fdata.html)](https://openfwi-lanl.github.io/docs/data.html#vel)\n\n<left width=\"100%\" style=\"padding:10px\"><img src=\"https://openfwi-lanl.github.io/assets/img/gallery.jpg\" width=\"600px\"></left>\n\n\n### Environment setup on your own machine\n\nAs we are using colab, you do not have to do anything currently. However later, if you would like to run experiments on your own machine, our codes mainly use the following packages:\n- pytorch v1.7.1\n- torchvisionv0.8.2\n- scikit learn\n- numpy\n- matplotlib","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}