<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bhavesh Patidar on Medium]]></title>
        <description><![CDATA[Stories by Bhavesh Patidar on Medium]]></description>
        <link>https://medium.com/@bhaveshpatidar?source=rss-bc2fd31f11c6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bpANY0PyjJL41lIf.</url>
            <title>Stories by Bhavesh Patidar on Medium</title>
            <link>https://medium.com/@bhaveshpatidar?source=rss-bc2fd31f11c6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 18:17:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bhaveshpatidar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Crop Classification with Multi-Temporal and Multi-Spectral Satellite Imagery]]></title>
            <link>https://bhaveshpatidar.medium.com/crop-classification-with-multi-temporal-and-multi-spectral-satellite-imagery-38afd59cb903?source=rss-bc2fd31f11c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/38afd59cb903</guid>
            <category><![CDATA[landcover-classification]]></category>
            <category><![CDATA[satellite-technology]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[agriculture]]></category>
            <dc:creator><![CDATA[Bhavesh Patidar]]></dc:creator>
            <pubDate>Sat, 04 Apr 2020 06:42:43 GMT</pubDate>
            <atom:updated>2020-11-22T05:43:21.753Z</atom:updated>
            <content:encoded><![CDATA[<h3>Crop Classification using multi-Spectral and multi-temporal satellite images</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*CVe4T9FfRMQ7Jl2GlYwQmw.png" /></figure><h3>Introduction</h3><p>In this blog, we will explore the multi-temporal and spectral satellite images and deep learning for crop classification. Crop classification is important for understanding the supplies of a crop. The satellite images can be helpful in monitoring crop growth and health in near real-time. Today, high-resolution satellite images are available at a daily frequency. With high-frequency data and multiple bands, it’s possible to classify crops using deep learning.</p><p>There are many classical machine learning approaches available to classify crops based on the spectral and textural properties of a crop but these methods aren’t that accurate and require a lot of feature engineering work, domain knowledge.</p><p>In this blog, we’ll use the method suggested by Rose. [1] which shows that crop’s properties change over time, where different crop types have unique growth cycles throughout the year. We’ll incorporate this temporal information into our model to get higher accuracy. The code is open-sourced on my <a href="https://github.com/bhavesh907/Crop-Classification">Github</a>.</p><h3><strong>Data Collection</strong></h3><p>For this blog, we will be using the RapidEye satellite images for the year 2017 provided by the planet.com and <a href="https://nassgeodata.gmu.edu/CropScape/">USDA Crop data layer 2017</a> to download the labeled crop images. Please make sure you have at least one cloud-free (&lt;5%) satellite image for each month. I’ve selected a small ROI in the California Fresno region for this blog and uploaded the dataset on Gdrive. To download the dataset, click <a href="https://drive.google.com/drive/folders/1go32yGsEoLkYQi48qVQoG7shDuTgfjf4?usp=sharing">here</a>.</p><h3>Model Building</h3><p>Let’s first read the USDA crop labels using the GDAL package and check the percentage of each crop. Our ROI has a total of 55 unique classes out of which we will select only the top 5 crops for the model building.</p><pre><strong>import</strong> <strong>numpy</strong> <strong>as</strong> <strong>np</strong><br><strong>from</strong> <strong>osgeo</strong> <strong>import</strong> gdal, gdal_array<br><strong>import</strong> <strong>pandas</strong> <strong>as</strong> <strong>pd</strong><br><strong>import</strong> <strong>operator</strong><br><strong>import</strong> <strong>gc</strong><br><strong>import</strong> <strong>os</strong><br><br>roi_ds = gdal.Open(&#39;./Dataset/cdl2017.tiff&#39;, gdal.GA_ReadOnly)<br><br>roi = roi_ds.GetRasterBand(1).ReadAsArray()<br><br><em># How many pixels are in each class?</em><br>classes = np.unique(roi)<br><br><br>dict = {}<br><strong>for</strong> c <strong>in</strong> classes:<br>    dict[c] = (roi == c).sum()<br>sorted_x = sorted(dict.items(), key=operator.itemgetter(1))<br>print(&quot;Top 6 classes and pixel counts <strong>\n</strong>&quot;,sorted_x[-6:])</pre><p>Now let’s read the 10 RapidEye satellite images and stack them in the columns in a pandas data frame. We have 10 images and 5 bands for each image so we’ll have a total of 50 columns in our data frame. For now, we’ll sample the 100,000 pixels for each class to avoid overfitting.</p><pre>images = [&#39;./Dataset/20170306.tiff&#39;,&#39;./Dataset/20170410.tiff&#39;,<br>          &#39;./Dataset/20170601.tiff&#39;,&#39;./Dataset/20170615.tiff&#39;,<br>          &#39;./Dataset/20170708.tiff&#39;,&#39;./Dataset/20170807.tiff&#39;,<br>          &#39;./Dataset/20170905.tiff&#39;,&#39;./Dataset/20170923.tiff&#39;,<br>          &#39;./Dataset/20171015.tiff&#39;,&#39;./Dataset/20171207.tiff&#39;]</pre><pre>final = pd.DataFrame()<br><br><strong>for</strong> c <strong>in</strong> top_classes:<br>    <br>    temp = pd.DataFrame()<br>    <br>    print(c)<br>    <br>    <strong>for</strong> img <strong>in</strong> images:<br><br>        print(img)<br><br>        train_ds = gdal.Open(img, gdal.GA_ReadOnly)<br><br>        print(train_ds.RasterXSize,train_ds.RasterYSize)<br><br>        img_b1 = np.zeros((train_ds.RasterYSize, train_ds.RasterXSize, train_ds.RasterCount),<br>                       gdal_array.GDALTypeCodeToNumericTypeCode(train_ds.GetRasterBand(1).DataType))<br>        <br>        <strong>for</strong> b <strong>in</strong> range(img_b1.shape[2]):<br>            img_b1[:, :, b] = train_ds.GetRasterBand(b + 1).ReadAsArray()<br>        <br>        print(img_b1.shape)<br><br><br>        Xt = img_b1[roi==c, :] <br>        <br>        Xt1 = pd.DataFrame(Xt)<br>        <br>        Xt2 = Xt1.sample(n=100000)<br>        <br>        Xt2.reset_index(drop=<strong>True</strong>,inplace=<strong>True</strong>)<br>        <br>        temp = pd.concat([Xt2,temp],axis=1)<br>        <br>        temp[&quot;class&quot;] = c<br>        <em>#temp.reset_index(drop=True,inplace=True)</em><br>      <br>    final = pd.concat([temp,final],axis=0)<br>    final.reset_index(drop=<strong>True</strong>,inplace=<strong>True</strong>)<br>    <br>    gc.collect()</pre><p>Now that we have the required data to train our models, we need to split it into the train, test sets, label encode the classes, and normalize it before feeding the data in the model.</p><pre><em># encode class values as integers</em><br>encoder = LabelEncoder()<br>encoder.fit(labels)<br>encoded_Y = encoder.transform(labels)<br><em># convert integers to dummy variables (i.e. one hot encoded)</em><br>dummy_y = keras.utils.to_categorical(encoded_Y,num_classes=5)</pre><pre>X_train, X_test, y_train, y_test = train_test_split(data, dummy_y, test_size=0.05, random_state=42,shuffle = <strong>True</strong>)</pre><pre>sc_X = StandardScaler()<br>X_train = sc_X.fit_transform(X_train)<br>X_test = sc_X.transform(X_test</pre><p>Now let’s define the model as suggested by Rose[1] and train it.</p><pre>n_classes = 5<br><br>model = Sequential()<br>model.add(Conv1D(kernel_size = 5, strides=1,filters = 32, activation=&#39;relu&#39;,input_shape=(50,1)))<br>                    <br>model.add(MaxPooling1D(pool_size = (2), strides=(2)))<br>model.add(Conv1D (kernel_size = 5, strides=1, filters = 64, activation=&#39;relu&#39;))</pre><pre>model.add(MaxPooling1D(pool_size = (2), strides=(2)))<br>model.add(Flatten())<br><br>model.add(Dense (1000, activation=&#39;relu&#39;))<br>model.add(Dense(n_classes, activation = &#39;softmax&#39;))</pre><pre>model.compile(loss=&#39;categorical_crossentropy&#39;, optimizer=keras.optimizers.Adam(lr=0.001),metrics=[&#39;accuracy&#39;])</pre><pre>X1 = np.expand_dims(X_train, axis=2)</pre><pre>model.fit(X1, y_train, epochs=10, batch_size=100)</pre><p>I’ve trained the model for 10 epochs with a batch size of 100. Let&#39;s make some predictions on a test set.</p><pre>X2 = np.expand_dims(X_test, axis=2)</pre><pre>scores = model.evaluate(X2, y_test)<br>25000/25000 [==============================] - 1s 41us/step</pre><pre>print(&quot;<strong>\n%s</strong>: <strong>%.2f%%</strong>&quot; % (model.metrics_names[1], scores[1]*100))</pre><pre>accuracy: 94.98%</pre><p>we got 94%+ accuracy on a test set. One can play with the hyperparameters and get better accuracy.</p><h3>Conclusion</h3><p>This blog shows the application of satellite images and Deep learning in agriculture. I hope you try the code for yourself and test it on more images.</p><p>Check <a href="http://neurafarms.ai/">http://neurafarms.ai/</a> for more interesting work in agriculture, If you have a project that we can collaborate on, then let us connect at hello@neurafarms.ai</p><h3>Reference</h3><p>[<a href="http://cs229.stanford.edu/proj2017/final-reports/5243811.pdf">1] </a>2017- Rose M. Rustowicz,” Crop Classification with Multi-Temporal Satellite Imagery”</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38afd59cb903" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>