Professional Documents
Culture Documents
ICT Competition Global Cloud 2021 2022 1
ICT Competition Global Cloud 2021 2022 1
Global Finals
Hands-on Practice
Contents
The smart city platform must provide data analysis and AI processing, including the following
features:
In this exam, use Huawei Cloud to build an architecture of cloud services, storage, big data,
and AI for a smart platform with these features.
2 Exam Description
2.1 Weighting
The exam consists of four parts: Cloud Services, Storage, Big Data, and AI. There are 1,000
points possible in this exam.
AI 15% 150
The exam account and password have been sent to your Email. For how to log in, see the
Exam Guide.
Cloud resources needed for the exam are already available. You will be informed of any
resources you will need to purchase on your own. When purchasing any resources, select
Pay-per-use for Billing Mode.
After completing operations on the ECS, do not stop the ECS.
If resources of a required flavor are sold out, purchase resources of a similar flavor.
3 Exam Questions
3.1 Cloud Services
3.1.1 Scenarios
A user deploys a WordPress website on Huawei Cloud in pursuit of website auto scaling,
operation log audit, data backup and migration.
Design an appropriate solution for this scenario and complete the following lab tasks.
The enterprise environment is established in AZ 1 of the Huawei Cloud Hong Kong region,
and an Elastic Cloud Server (ECS) is used to set up the WordPress website. Distributed
Database Middleware (DDM) will be used to connect Relational Database Service (RDS) DB
instances. Auto Scaling (AS) and Elastic Load Balance (ELB) are used to ensure availability and
reliability in the same AZ. Log Tank Service (LTS) and CES are used to store and monitor the
website, and Object Storage Service (OBS) is used to store website data.
You need to set up an ECS ecs-migration in VPC-migration, and use VPC peering connection
to connect VPC-wordpress and VPC-migration. You also need to migrate the source server
wordpress-server to ecs-migration, create a NAT gateway for ecs-migration and configure
rules so that ecs-migration can be accessed over the Internet.
The cloud resources listed in the following table are for you to purchase.
3.1.4.3 Tools
For this test, you will need the tool described in the table here.
Tool Remarks
Instance Nodes 1
Screenshot requirements:
a. Take a screenshot of the basic information of ddm-wordpress and save it as 1-1-
1ddm.
Subtask 2: Create a schema and an account.
a. Create a schema based on ddm_wordpress, and set the following parameters. For
parameters that are not specified, use the default settings.
Sharding Unsharded
Username ICT_user##
Password ict2022@ICT##
Schema db_wordpress
Permissions All
Screenshot requirements:
a. Take a screenshot of the page showing the schema is in the Running state and save
it as 1-2-1schema.
b. Take a screenshot of the DDM account created and save it as 1-2-2account.
Subtask 3: Create an ECS to access WordPress.
a. Create an ECS named wordpress-server with the following details. For parameters
that are not specified, use the default settings.
VPC vpc-wordpress
Password ict2022@ICT##
b. Click Submit, and click Run the installation, and then enter the following
information. And click Log In.
Screenshot requirements:
a. Take a screenshot of the database configuration page and save it as 1-4-1ddm
configuration.
b. Take a screenshot of the page showing successful access to WordPress and save it as
1-4-2wordpress.
b. Create an auto backup policy named policy-auto with the following details.
Task 3: Configuring website load balancing and auto scaling (120 points)
Subtask 1: Create an ELB load balancer.
a. Unbind the EIP from wordpress_server.
b. Buy an ELB load balancer and set the following parameters. For parameters that are
not specified, use the default settings.
Type Shared
Name elb-wordpress
c. Configure a listener named listener-wordpress and set the frontend protocol and
port to TCP and 80.
d. Configure a backend server group named server_group-wordpress. Do not enable
health check and retain the default settings for other parameters.
e. Add the backend server wordpress-server and configure the backend port to 80.
f. Access http://EIP of elb-wordpress/wordpress to verify that the configuration was
successful.
Screenshot requirements:
a. Take a screenshot of wordpress-server details after you unbind its EIP and save it as
3-1-1wordpress-server-noeip.
b. Take a screenshot of elb-wordpress in the load balancer list and save it as 3-1-2elb-
wordpress.
c. Take a screenshot of the basic information about listener-wordpress and save it as
3-1-3listener.
d. Take a screenshot of the basic information about server_group-wordpress and save
it as 3-1-4server group.
e. Access http://EIP of elb-wordpress/wordpress, take a full-page screenshot (including
the IP address in the browser) and save it as 3-1-5elb-wordpress.
Note: The IP address in the browser must be the same as the EIP of elb-wordpress.
Subtask 2: Create an AS group and configure scaling rules.
a. Create an AS configuration named as-config-wordpress with the following details.
For parameters that are not specified, use the default settings.
Name as-config-wordpress
Password ict2022@ICT##
b. Create an AS group with the following details. For parameters that are not specified,
use the default settings.
Name as-group-wordpress
Max. Instances 4
Expected 1
Instances
Min. Instances 1
VPC VPC-wordpress/subnet_wordpress
Trigger Condition The maximum CPU usage is greater than or equal to 40%.
Consecutive 1
Occurrences
Screenshot requirements:
a. Take a screenshot of the as-config-wordpress list and save it as 3-2-1as-config-
wordpress.
b. Take a screenshot of the as-group-wordpress overview and save it as 3-2-2as-
group-wordpress.
c. Take a screenshot of the instance details of as-group and save it as 3-2-3as-
instances.
d. Take a screenshot of the as-policy-expanding list and save it as 3-2-4as-policy-
details.
Subtask 3: Verify the auto scaling.
a. Manually scale out the scaling group. Set Expected Instances to 4, view the AS
instance list and verify the results.
b. Manually scale in the scaling group. Set Expected Instances to 2, view the AS
instance list and verify the results.
c. Verify the AS policy. Manually execute as-policy-expanding.
d. During the auto scaling, view the AS group information on the Monitoring tab page
of as-group-web.
Note: After you set the Expected Instances, please wait for a few minutes until it has
been changed and you can view the results. After you can view the results, go the next step.
Screenshot requirements:
a. Take a screenshot of Scaling Action tab page of as-group-web after the verification
is complete and save it as 3-3-1scaling-actions.
b. After you finish the steps above, wait for at least 10 minutes until the change of
instances take effect in the Monitoring tab page, and then take a screenshot of all
monitoring curves on the Monitoring tab page of as-group-web and save it as 3-3-
2as-group-web-monitoring.
b. Take a screenshot of the alarm record page and save it as 4-2-2history alarm.
Subtask 3: Configure a log monitoring policy for ECSs.
a. Create a log group named lts-group-wordpress and set the logs to be retained for 7
days.
b. Create a log stream named lts-topic-wordpress in lts-group-wordpress.
c. Install ICAgent for wordpress-server with the following details:
OS: Linux
Installation Mode: Obtain AK/SK
You can obtain the AK/SK from the My Credentials page of your exercise account.
d. Configure log ingestion. On the Select Ingestion Type page, select Host, and then
select the newly created log group and log stream. Create a host group and
configure collection.
Host OS Linux
e. On the Log Management page, click the log group name and log stream name,
select Original Log, and enter successful in the text box to search for related logs.
Screenshot requirements:
a. Take a screenshot of the page for creating the log stream and save it as 4-3-1Log
Stream.
b. Take a screenshot of the host list displayed on the Hosts tab and save it as 4-3-
2ICAgent Hosts.
c. Click the ingestion configuration name, take a screenshot of configuration details and
save it as 4-3-3collect-wordpress.
d. Take a screenshot of the page where you search for successful logs and save it as 4-
3-4realtime log.
Subtask 4: Transfer logs to OBS.
a. Create a log transfer rule as the following parameters. Retain the default settings for
other parameters.
b. Log in to OBS Console and view the log folder automatically uploaded to the bucket.
Screenshot requirements:
a. Take a screenshot of the log transfer and save it as 4-4-1logs-transfer-config.
b. After a few minutes, take a screenshot of logs stored in the OBS bucket and save it
as 4-4-2logs-in-bucket.
VPC vpc-migration
Specifications 2 vCPUs | 4 GB
Password ict2022@ICT##
Screenshot requirements:
a. Take a screenshot of ecs-migration details and save it as 5-3-1wordpress-server.
Subtask 4: Migrate wordpress-server to another VPC.
a. Bind an EIP to wordpress_server again.
b. Install the migration Agent on wordpress-server. For details about the installation,
see Appendix 3.
c. On the Servers page of Server Migration Service (SMS), you can see wordpress-
server with the Agent installed.
d. Configure the target server with the following details. For parameters that are not
specified, use the default settings.
Network Private
Partition Resizing No
Note: The migration may take a few minutes. You can come back to take the screenshot
showing a successful migration after the migration is complete.
Screenshot requirements:
a. After Agent is installed, take a screenshot containing the "sms agent start up
successfully!" and save it as 5-4-1agent.
b. After the migration is done, take a screenshot of the migration subtask page and
save it as 5-4-2migration task.
Appendix 3
1. Log in to wordpress-server.
2. Download and install Agent and rsync:
wget -t 3 -T 15 https://sms-agent-2-0.obs.ap-southeast-1.myhuaweicloud.com/SMS-
Agent.tar.gz
wget -t 3 -T 15 https://sms-agent-2-0.obs.ap-southeast-1.myhuaweicloud.com/SMS-
Agent.tar.gz.sha256
yum install -y rsync
tar -xvf SMS-Agent.tar.gz
cd SMS-Agent
./startup.sh
Enter y, access key ID, and secret access key as prompted.
Note: You can create an access key by choosing your test account > My Credentials >
Access Keys and saving the access key file to your computer.
Subtask 5: Configure a NAT gateway.
In this subtask, you need to create a NAT gateway and configure a DNAT rule for ecs-
migration in the VPC-migration subnet to be accessed over the Internet.
a. Buy a public network NAT gateway with following details..
Name NATGW-HK
VPC vpc-migration
Subnet subnet-migration
Type Small
Billed By Traffic
Bandwidth 5 Mbit/s
Quantity 2
Scenario VPC
Protocol All
EIP bandwidth-hk2
3.2 Storage
3.2.1 Scenarios
To permanently store data on the Smart City platform and offer massive storage, a storage
solution needs to be established at the platform's bottom layer. In addition, a remote
replication disaster recovery solution is required to guarantee high reliability.
3.2.4.2 Tools
Tool Description
a. Log in to the web management page of two storage devices individually. The username
and password for logging in to the storage device are admin and ict2022@ICT##.
b. Set the location of estor1 to HongKong1 and estor2 to HongKong2.
c. Create a storage pool and import the license. Store the license in an OBS bucket on the
cloud. The path is obs-ict-resource >> storage >> License_V6 to 2022.10.1.rar.
Screenshot requirements:
a. After the license has been imported to both storage devices, take screenshots of license
lists and name them 1-1-1initial-lisence1 and 1-1-2initial-lisence2.
b. Take screenshots of the Device Manager home pages of both storage devices and name
them 1-1-3initial-finished1 and 1-1-4initial-finished2.
Screenshot requirements:
a. Take a screenshot of the mapping between WindowsStorage1 and Win1LUN and name
it 1-2-1map-win.
b. Take a screenshot of the mapping between LinuxStorage and LinLUN and name it 1-2-
2map-linux.
a. Based on the topology, create a logical service port on estor1 and name it ServicePort1.
b. Based on the topology, create a logical service port on estor2 and name it ServicePort2.
Screenshot requirements:
Screenshot requirements:
d. Take a screenshot of the Huawei.txt file on the WindowsStorage1 host and name it 2-
2-4win-txt1.
Screenshot requirements:
a. After successfully logging in to the target on the host, take a screenshot and name it 2-3-
1lin-load.
b. Take a screenshot of the Huawei.txt file and name it 2-3-2lin-txt2.
a. Based on the topology, create a logical port of file services on estor1 and name it
FileService.
Screenshot requirements:
a. On estor1, create a file system for Windows and name it in the format of ICT<Exam
group number> (for example, ICT01). Set the file system size to 3 GB and application
type to NAS_Default.
b. Create a local Windows authentication user with the username in the format of
ICT<Exam group number> (for example, ICT01) and password setting to ict2022@ICT##,
add the user to Users group.
c. Create a share and name it in the format of ICT<Exam group number> (for example,
ICT01). Add the created authentication user to this share and grant the user with the full
control permission.
Screenshot requirements:
a. Take a screenshot of the details of the created file system and name it 3-2-1cifs-
filesystem.
b. Take a screenshot of the details of the created authentication user and name it 3-2-2cifs-
authuser.
c. Take a screenshot of the details of the created share and name it 3-2-3cifs-share.
a. Log in to WindowsStorage1 and access the shared space using the authentication user
created in the previous step.
b. Create a .txt file in the filesystem and name it HuaweiFile.
Screenshot requirements:
a. After successfully logging in, take a screenshot of the shared space and name it 3-3-
1share-filesystem.
b. After successfully creating the HuaweiFile file, take a screenshot and name it 3-3-
2share-txt.
Procedure:
a. Create logical replication ports on estor1 and estor2 and name them RepPort1 and
RepPort2.
Screenshot requirements:
a. Take screenshots of the RepPort1 and RepPort2 ports and name them 4-1-1rep-port1
and 4-1-2rep-port2. Ensure that the screenshots contain details such as IP Address, Home
Port, and IP Address Failover.
a. Create a remote device administrator on estor2. The username and password are
RemoteUser and ict2022@ICT##.
b. On estor1, add estor2 as the remote device.
Screenshot requirements:
a. View the details of the remote device administrator on estor2, take a screenshot, and
name it 4-2-1remote-user.
b. View the details of the remote device on estor1, take a screenshot, and name it 4-2-
2remote-device.
a. Create a remote replication pair on estor1 based on Win1LUN. Major parameters are as
follows:
Replication Mode: Asynchronous
Pair Creation: Automatic
Synchronization Type: Manual
Screenshot requirements:
a. Take a screenshot of the details of the remote replication pair on estor1 and name it 4-3-
1rep-pair.
Screenshot requirements:
a. After successfully splitting the pair, take a screenshot of the details of the remote
replication pair on estor1 and name it 4-4-1rep-split.
b. Log in to WindowsStorage2, check the data on the disk mapped from the secondary
LUN, take a screenshot, and name it 4-4-2rep-verify. Ensure that the screenshot contains
the host IP address.
c. Go back to estor2, view details of the remote replication pair, take a screenshot, and
name it 4-4-3rep-pro. Ensure that the screenshot contains the synchronization start time,
synchronization end time, and secondary resource protection status.
unavailable, ask one of the exam supervisors for help. Obtain the datasets required in exam
from the corresponding OBS bucket.
3.3.3.3 Tools
Tool Remarks
Note: To ensure that your answer can be scored, take screenshot of your code or outputs as
required.
b. Create a database named ict_bigdata and set the character set to UTF-8.
Screenshot requirements:
a. Take a screenshot after the login and save it as 1-1-1login.
b. Take a screenshot of the created database and save it as 1-1-2mysqldatabase.
Subtask 2: Create a MySQL table.
a. Create a table named confirmed in the ict_bigdata database. Set the table engine to
InnoDB and the character set to UTF-8. Set the field names to: date, confirmed,
Province/State, and Country/Region. Set the field types, respectively, to:
varchar(50), int(11), varchar(50), and varchar(50).
None fields can be empty. No primary key is required. Create a multi-column index
for the date, Province/State and Country/Region columns. The relationships would
be as follows:
Field Type Null Key Default Extra
date varchar(50) NO MUL
confirmed int(11) NO
Province/State varchar(50) NO
Country/Region varchar(50) NO
b. Create a table named recovered in the ict_bigdata database. Set the table engine to
InnoDB and the character set to UTF-8. Set the field names to: date, recovered,
Province/State, and Country/Region. Set the field types, respectively, to:
varchar(50), int(11), varchar(50), and varchar(50).
None fields can be empty. No primary key is required. Create a multi-column index
for the date, Province/State and Country/Region columns. The relationships are as
follows:
Field Type Null Key Default Extra
date varchar(50) NO MUL
recovered int(11) NO
Province/State varchar(50) NO
Country/Region varchar(50) NO
c. Create a table named deaths in the ict_bigdata database. Set the table engine to
InnoDB and the character set to UTF-8. Set the field names to: date, deaths,
Province/State, and Country/Region. Set the field types, respectively, to:
varchar(50), int(11), varchar(50), and varchar(50).
None fields can be empty. No primary key is required. Create a multi-column index
for date, Province/State and Country/Region columns. The relationships are as
follows:
Field Type Null Key Default Extra
date varchar(50) NO MUL
deaths int(11) NO
Province/State varchar(50) NO
Country/Region varchar(50) NO
Screenshot requirements:
a. Take a screenshot of the SQL statement used to create the confirmed table and save
it as 1-2-1confirmed.
b. Take a screenshot of the SQL statement used to create the recovered table and save
it as 1-2-2recovered.
c. Take a screenshot of the SQL statement used to create the deaths table and save it
as 1-2-3deaths.
Subtask 3: Import data.
Use the data import function (shown in the red box in the following figure) to import the
three .csv files in the obs_ictbigdata bucket to the tables you created in subtask 2.
Screenshot requirements:
a. Take a screenshot after importing data and save it as 1-3-1importdata.
Subtask 4: Join multiple MySQL tables and query data.
a. Join the three tables you created through the date, Province/State, and
Country/Region fields, and save the results to table data, where the fields are date,
confirmed, recovered, deaths, Province/State, Country/Region in sequence. The
configuration of the table fields is as follows:
Field Type Null Key Default Extra
date varchar(50) NO
confirmed int(11) NO
recovered int(11) NO
deaths int(11) NO
Province/State varchar(50) NO
Country/Region varchar(50) NO
b. Query the data whose Country/Region is Australia, China, France, US, and United
Kingdom from the data table and save the returned data to the mysql_result table.
Ensure that the field sequence in the mysql_result table is the same as that in the
data table.
Screenshot requirements:
a. Take a screenshot of the first 50 rows of the data table and save it as 1-4-1joined.
b. Take a screenshot of the first 50 rows of the mysql_result table and save it as 1-4-
2filtered.
Task 2: Creating a Hive database and table and importing data from HDFS
(60 points)
Subtask 1: Export the MySQL result and upload it to HDFS.
a. On the RDS for MySQL page, choose Import and Export and click Export. On the
displayed data export page, click Create Task to export the mysql_result table from
the ict_bigdata database to the obs_ictbigdata bucket in the CSV format. Retain all
data and set the encoding format to UTF-8. You do not need to retain field
attributes. Take a screenshot after exporting the data and download the exported
data to a local folder.
b. Use PuTTY to remotely log in to the MRS server as root (password: ict2022@ICT##).
In the /user directory of HDFS, create a subdirectory with the same name as the IAM
username of your group, for example: /user/ict11.
c. Decompress the .zip file downloaded in step a and use WinSCP to upload the .csv file
to the MRS cluster. Run the following shell command in PuTTY to remove the
quotation marks from the file.
d. Use PuTTY to upload the processed file to the HDFS directory you created in step b.
Screenshot requirements:
a. After exporting the data, take a screenshot and save it as 2-1-1exportdata.
b. Take a screenshot of the created HDFS directory and save it as 2-1-2hdfsmkdir.
c. No screenshot required
d. Take a screenshot of the shell commands you used for uploading the file to HDFS
and checking whether the file upload is successful and save it as 2-1-3hdfsupload.
Subtask 2: Create a database and an external table in Hive and import data.
a. Run the beeline command to enter the Hive CLI; create a database named
ict_bigdata in Hive; display all databases in the current instance; and switch to the
ict_bigdata database.
b. Create an external table named ods_covid19 in the ict_bigdata database. Set the
field names and field types listed in the following table. Retain the default values for
other parameters.
Field Type
date string
confirmed int
recovered int
deaths int
Province/State string
Country/Region string
c. Run the Hive command to import the mysql_result.csv file in HDFS to the external
table ods_covid19. After the data is imported successfully, query the first 30 rows of
the ods_covid19 table.
Screenshot requirements:
a. Take a screenshot of the SQL statement you used and save it as 2-2-1hivedatabase.
b. Take a screenshot of the SQL statement you used for creating the table and the
creation results. Save the image as 2-2-2ods_covid19.
c. Take a screenshot of the first 30 rows of the ods_covid19 table and save it as 2-2-
3loaddata.
Task 3: Building a layered model and analyzing statistics in Hive (60 points)
Subtask 1: Query Hive data.
a. Use PuTTY to log in to the MRS server and go to Hive. Switch to the ict_bigdata
database in Hive and create a table named dwd_covid19 in the database. Set the
field names and field types listed in the following table.
Field Type
year string
month string
day string
confirmed int
recovered int
deaths int
Province/State string
Country/Region string
b. Split data (Year/Month/Day) in the date column of ods_covid19 table to the first
three columns of the dwd_covid19 table in sequence. View the first 30 rows of the
dwd_covid19 table.
c. Create a table named dws_monthly_confirmed in the ict_bigdata database. Set the
field names and field types listed in the following table.
Field Type
year string
month string
Country/Region string
monthly_confirmed int
d. Query the monthly confirmed cases of the specified five countries in the
dwd_covid19 table and save the returned data to the dws_monthly_confirmed
table.
Screenshot requirements:
a. Take a screenshot of the SQL statement you used for creating the dwd_covid19
table and save it as 3-1-1dwd_covid19.
b. Take a screenshot of the first 30 rows of the dwd_covid19 table and save it as 3-1-
2separated.
c. Take a screenshot of the SQL statement you used for creating the
dws_monthly_confirmed table and save it as 3-1-3dws_monthly_confirmed.
d. Take a screenshot of the dws_monthly_confirmed table after importing data and
save it as 3-1-4monthlyconfirmed.
Subtask 2: Use the Hive window function to query data.
a. Create a table named dws_most_recovered in the ict_bigdata database. Set the
field names and field types listed in the following table.
Field Type
year string
month string
Country/Region string
monthly_recovered int
rank int
b. For each month, query the two countries with the largest number of monthly
recoveries in the dwd_covid19 table and save the returned data to the
dws_most_recovered table.
Screenshot requirements:
a. Take a screenshot of the SQL statement you used for creating the
dws_most_recovered table and save it as 3-2-1dws_most_recovered.
b. Take a screenshot of the dws_most_recovered after importing data and save it as 3-
2-2mostrecovered.
a. Use PuTTY to log in to the MRS server, and run the HDFS command to download the
000000_0 file in the
/user/hive/warehouse/ict_bigdata.db/dws_monthly_confirmed directory to the
local PC, and save the file to your desktop using WinSCP.
b. Go to the ModelArts console and choose DevEnviron > Notebook. Click Open
JupyterLab to enter the exam environment.
d. Drag the 000000_0 file on the desktop to the list on the left to upload the data.
e. Read the 000000_0 file and create a Spark DataFrame object to save the data. Just
copy and paste the following code to the CLI:
import pandas as pd
data_pd=pd.read_csv('000000_0',header=None)
data_pd.columns=['year','month','country/region','monthly_confirmed']
data_origin = spark.createDataFrame(data_pd)
data_origin.show()
f. Run the code shown here to construct features. This code is used to query data
whose Country/Region field in the data_origin table is US. Add a feature column
whose value increments from 1 (the month since 2020), and then extract the data in
the monthly_confirmed field. Export the data to a new field named label and export
the returned data to a new table named data.
import pyspark.sql.functions as F
data = data_origin.filter("`country/region`=='US'")
w = Window.orderBy('year','month')
g. Import dependencies:
h. Use the VectorAssembler function to convert the feature column in the data table
into the vector format and output a new column named FeatureVec. Retain the new
table name as data. In the new data table, the FeatureVec column is added. You
only need to copy and paste the following code to the CLI:
data = VectorAssembler(inputCols=['feature'],outputCol='FeatureVec').transform(data)
Screenshot requirements:
Take a screenshot after uploading data (in step d) and save it as 4-1-1uploaded.
Subtask 2: Analyze and visualize the regression using PySpark.
a. Use the data.randomSplit method to divide the data in the data table into the
training set trainingData and the test set testData (proportion: 20%) and set the
seed of random number to 0 (seed=0).
b. Use the RandomForestRegressor method to train a random forest regression model;
specify featuresCol='FeatureVec' retain the default values for other parameters; and
train trainingData.
Name the trained model model.
c. Use model to perform prediction on testData.
Name the output predictions and display the prediction output.
d. Use the RegressionEvaluator method and specify its parameters labelCol="label",
predictionCol="prediction", metricName="rmse" to compare the real value label
and the prediction value prediction.
Save the results to variable rmse and run the following command to output the
results:
print("Root Mean Squared Error (RMSE) on test data = %g" % rmse).
e. Use model to fit the full data in data.
Name the output full_predict and print it.
f. Use the toPandas method to convert full_predict to Pandas DataFrame. Use the
matplotlib.pyplot module to draw a line chart with the feature column in
full_predict as the X-axis and the label and prediction columns as the Y-axes. The
legends are true_case and predicted_case. The line will look something like this:
Screenshot requirements:
a. Take a screenshot of the code you used for splitting the dataset and save it as 4-2-
1randomsplit.
b. Take a screenshot of the code you used for training the model and save it as 4-2-
2model.
c. Take a screenshot of the code you used in this step and save it as 4-2-3testpredict.
d. Take a screenshot of the code you used in this step and save it as 4-2-4evaluate.
e. Take a screenshot of the code you used in this step and save it as 4-2-5fullpredict.
f. Take a screenshot of the code you used and the output line chart and save it as 4-2-
6visualization.
3.4 AI
3.4.1 Scenarios
AI applications require a lot of high quality data. To obtain enough high-quality and class-
balanced samples, neural networks can be used to generate new data based on existing data
and to expand datasets.
3.4.3.1 Tools
Tool Description Available At
Data sample
If you do not strictly follow the preceding instructions, you may fail to earn any points.
Screenshot requirements:
a. Take a screenshot of the code you used and the summary result of the generator.
Save the image as 2-1-1generator.
Subtask 2: Build a discriminator.
a. Set up a discriminator neural network based on the summary result listed in the
figure below. Ensure that the input of the discriminator is the same as the output of
the generator.
b. Use sigmoid activation for the last fully connected layer. For other layers, set
hyperparameter alpha of the LeakyReLU activation function to 0.2.
c. Compile the model with binary cross-entropy loss and Adam optimizer. Set
hyperparameter lr to 0.002, beta_1 to 0.5, and the model evaluation indicator to
accuracy. Print the discriminator network information.
Screenshot requirements:
a. Take a screenshot of the code you used and the returned output and save it as 2-2-
1descriminator
Subtask 3: Combine the generator and discriminator to construct a GAN.
a. Connect the generator and discriminator based on the summary result listed in the
figure below; define the GAN input as the generator input; send the images
generated by the generator to the discriminator; and define the GAN output as the
discriminator output.
b. Compile the model with binary cross-entropy loss function and Adam optimizer. Set
hyperparameter lr to 0.005, beta_1 to 0.5, and the model evaluation indicator to
accuracy.
Screenshot requirements:
a. Take a screenshot of the code you used and the returned output and save it as 2-3-
1GAN.
a. Generate the Gaussian noise and input the noise to the generator to generate
images.
b. Switch the pixel value of the images back to (0,255) and convert the vector to a 64 x
64 matrix. Randomly print ten grayscale images.
c. Obtain the generator and discriminator losses from the loss dictionary to construct a
loss visualization function.
d. Label the two types of losses. Set the X axis to the training epoch and the Y axis to
the loss. A diagram legend must be included.
Screenshot requirements:
a. Take a screenshot of the code you used and the returned output and save it as 3-2-
1loss
Subtask 2: Model training.
a. Create a training loop, set the number of model training epochs to 10 and the
training batch size to 32. Randomly extract images from the dataset for training in
batches, and label the real images with a 1.
b. Fix the discriminator to create a generator and input Gaussian noise to generate fake
images. Then label the fake images with a 0 and obtain the loss of the generator.
c. Unfix discriminator parameters and use the discriminator to determine whether an
input image is true or fake. The discriminator loss is the average value of the true
images identification loss and fake images identification loss.
d. Build a loss dictionary for storing the generator and discriminator losses occurred in
each batch.
Screenshot requirements:
a. Take a screenshot of the code you used and the returned output and save it as 3-1-
1model-training-10.
Subtask 3: Visualize the training results.
a. Call the images generation and losses visualization functions built in subtask 1.
b. Print the images generated by GAN every ten epochs. Print the loss curves chart of
the generator and discriminator every ten epochs.
Screenshot requirements:
a. Take a screenshot of the code you used and the returned output and save it as 3-3-
1visualization.
Appendix 4
Generate images of animated characters images through GAN.
This example uses Keras to build a generator and discriminator, and it uses an animation
dataset to train a model for generating animation character images. The generated images
can be used for subsequent model training.
image_size=64
input_images=[]
for file in filename:
# Read an image.
img=Image.open(dataset_dir+"/"+file)
# Resize all images to 64 x 64 pixel.
img=img.resize((image_size, image_size))
# Convert all images to grayscale.
img=img.convert("L")
# Convert all images into a NumPy array.
img=np.asarray(img)
# Put all images in the list.
input_images.append(img)
# Convert the image list into a NumPy array.
input_images = np.asarray(input_images)
print(input_images.shape)
# Output results
(256, 64, 64)
#Create a canvas and print four images.
plt.figure()
for i in range(4):
plt.subplot(2,2,i+1)
plt.imshow(input_images[i])
plt.show()
# Output results
3. Define a generator.
# Define a generator g.
g = Sequential()
# Create a fully connected layer with 1024 neurons. Set the input layer size same as the noise vector length.
g.add(Dense(1024, input_dim=noise_length))
# Use LeakyReLU as the activation function. When x < 0, y = 0.2x.
g.add(LeakyReLU(alpha=0.2))
# Add the BN layer to reduce the center offset.
g.add(BatchNormalization(momentum=0.8))
# Add the second fully connected layer with 4096 neurons. Use tanh as the activation function.
g.add(Dense(4096))
# Select LeakyReLU as the activation function. When x < 0, y = 0.2x.
g.add(LeakyReLU(alpha=0.2))
# Add the BN layer to reduce the center offset.
g.add(BatchNormalization(momentum=0.8))
# Add an output layer. Each output image contains 4096 pixels. Use the tanh activation function to map the output to
(-1, 1).
g.add(Dense(4096))
# Output result
4. Define a discriminator.
# Set the optimizer of the discriminator to Adam. Set a relatively small learning rate.
d_adam = Adam (lr = 0.0002, beta_1 = 0.5)
# Construct a discriminator.
d = Sequential()
# Set the number of neurons at the discriminator input layer as same as that of the generator output layer.
d.add(Dense(1024, input_dim = 4096))
# Select LeakyReLU as the activation function.
d.add(LeakyReLU())
# Create the next fully connected layer with 256 neurons.
d.add(Dense(256))
# The discriminator output layer has only one neuron to identify whether an image is true or fake. Use sigmoid to map
the output of the discriminator to (0,1). 0 indicates fake and 1 indicates true.
d.add (Dense (1, activation = 'sigmoid'))
# The discriminator uses the cross entropy loss function and sets accuracy as an indicator.
d.compile (loss = 'binary_crossentropy', optimizer = d_adam, metrics = ['accuracy'])
# Print the discriminator information.
d.summary ()
# Output results
#Output results
# Set the discriminator loss to the average value of the true images identification loss and fake images
identification loss.
d_loss = 0.5*np.add(d_loss_true, d_loss_fake)
# Add the discriminator loss of the current training batch to the loss dictionary.
losses["D"].append(d_loss)
# Add the generator loss of the current training batch to the loss dictionary.
losses["G"].append(g_loss)
# Print the generated images for each five training batches.
if epochs%10==0:
plot_generated()
# Print the loss and accuracy value.
print('-'*5, 'Epoch %d' % epochs, '-'*5)
print("Generator Loss:",g_loss[0])
print("Discriminator Loss:",d_loss[0])
if epochs%10==0:
plot_loss(losses)
#Output results