Exporting your earth Engine training data to Tensorflow records

Using the JavaScript code editor

aiworkflowExport2.png

In order to build your Tensorflow model you will need to export your training data to your cloud bucket or google drive. An example is shown below. Find the link here.

// set variables
var bandsIn = ee.List(['B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B10','B11','B12']);
var bandsOut = ee.List(['cb','blue','green','red','re1','re2','re3','nir','re4','waterVapor','cirrus','swir1','swir2']);
var label = 'land_class';

var referenceData = ee.FeatureCollection("projects/servir-mekong/Temp/trainingDataHanoi");
var image = ee.Image("COPERNICUS/S2/20171220T034151_20171220T034145_T48QWJ").select(bandsIn,bandsOut).divide((10000));

// Filter the different categories
var urban = referenceData.filter(ee.Filter.eq("land_class",0));
var water = referenceData.filter(ee.Filter.eq("land_class",1));
var barren = referenceData.filter(ee.Filter.eq("land_class",2));
var forest = referenceData.filter(ee.Filter.eq("land_class",3));
var cropland = referenceData.filter(ee.Filter.eq("land_class",4));

// calculate ndvi and add bands
var ndvi = image.normalizedDifference(['nir','red']).rename(['ndvi']);
var image = image.addBands(ndvi);

// sample the data
var sample = image.sampleRegions({collection:referenceData, properties:[label], scale:10}).randomColumn();

// Partition the sample approximately 70-30;
var training = sample.filter(ee.Filter.lt('random', 0.7));
var testing = sample.filter(ee.Filter.gte('random', 0.7));

// Export the Data
var outputBucket = "yourBucket";
var trainFilePrefix = "Hanoi/trainFile";
var testFilePrefix = "Hanoi/testFile";

// Create the tasks.
Export.table.toCloudStorage({
collection:training,
description:'Training_Export',
fileNamePrefix:trainFilePrefix,
bucket:outputBucket,
fileFormat:'TFRecord'});

Export.table.toCloudStorage({
collection:testing,
description:'Testing_Export',
fileNamePrefix:testFilePrefix,
bucket:outputBucket,
fileFormat:'TFRecord'});

// add data to map
Map.addLayer(image,{min:0,max:0.60,bands:['swir1','nir','red']},"Image");
Map.addLayer(urban.draw("red"),{},"Urban");
Map.addLayer(water.draw("blue"),{},"Water");
Map.addLayer(barren.draw("brown"),{},"Barren");
Map.addLayer(forest.draw("darkgreen"),{},"Forest");
Map.addLayer(cropland.draw("green"),{},"Cropland");

 

One comment

Leave a Reply